Вы можете добавить invalid_raise = False
, чтобы пропустить ошибочные строки, или usecols=np.arange(0, 3)
, однако я бы выбрал следующий подход:
list.txt:
1 9 1 6 "Thu Feb 13 13:12:30 2014 "
0 0 0 0 0 0
38 38 915 915
"CJE "
"2 "
"110321-025-01D-1ST
0 0 1 .1 73.7972 17 50
1 0 7 1 60 0 0 0 0
0 " "
1 0
#
38 38 No Data
39 38 No Data
40 38 No Data
41 38 3
42 38 No Data
43 38 4
44 38 4
45 38 5
, а затем:
logFile = "list.txt"
# opening the file
with open(logFile) as f:
#reading the lines after slicing it i.e. 11
content = f.readlines()[11:]
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
# for each line in content
for line in content:
# if the line has No Data in it
if line.find("No Data"):
# Replacing the No Data with 0 using replace() method
line = line.replace("No Data", "0")
print(line)
ВЫХОД:
38 38 0
39 38 0
40 38 0
41 38 3
42 38 0
43 38 4
44 38 4
45 38 5
РЕДАКТИРОВАТЬ:
, чтобы добавить их в матрицу из 3 столбцов:
_list = []
# for each line in content
for line in content:
# if the line has No Data in it
if line.find("No Data"):
# Replacing the No Data with 0 using replace() method
line = line.replace("No Data", "0")
# print(line)
# list comprehension for splitting on the basis of space and appending to the list
_list.append([e for e in line.split(' ') if e])
print(_list)
ВЫХОД:
[['38', '38', '0'], ['39', '38', '0'], ['40', '38', '0'], ['41', '38', '3'],
['42', '38', '0'], ['43', '38', '4'], ['44', '38', '4'], ['45', '38', '5']]
РЕДАКТИРОВАТЬ 2:
чтобы удалить последнюю строку в вашем файле, вы можете использовать нарезку content[:-1]:
:
logFile = "list.txt"
# opening the file
with open(logFile) as f:
#reading the lines after slicing it i.e. 11
content = f.readlines()[11:]
_list = []
# for each line in content
for line in content[:-1]:
# if the line has No Data in it
if line.find("No Data"):
# Replacing the No Data with 0 using replace() method
line = line.replace("No Data", "0")
# list comprehension for splitting on the basis of space and appending to the list
_list.append([e for e in line.strip().split(' ') if e])
print(_list)
ВЫХОД:
[['38', '38', '0'], ['39', '38', '0'], ['40', '38', '0'], ['41', '38', '3'],
['42', '38', '0'], ['43', '38', '4'], ['44', '38', '4'], ['45', '38', '5']]