我试图从表格文件中获取价值。
filename = open('D:\THESIS\DATA\outputData\covarince.txt', 'r')
for n in xrange(21):
next(filename)
#for line in filename:
for line in filename:
line = line.strip()
word = line.split()
print word [5:9]
但结果......
['1.858598e+007', '1.771380e+007', '1.680333e+007', '3.094793e+007']
['1.755510e+007', '1.675123e+007', '1.592444e+007', '2.924262e+007']
['1.667081e+007', '1.593449e+007', '1.525907e+007', '2.744351e+007']
['3.140037e+007', '2.997102e+007', '2.821130e+007', '5.446339e+007']
['2.610668e+007', '2.504934e+007', '2.423942e+007', '4.068118e+007']
['2.504934e+007', '2.410118e+007', '2.337335e+007', '3.907932e+007']
['2.423942e+007', '2.337335e+007', '2.292371e+007', '3.696649e+007']
['4.068118e+007', '3.907932e+007', '3.696649e+007', '7.047854e+007']
[]
[]
[]
[]
[]
['4', '5', '6', '7']
[]
['0.83751', '0.83075', '0.80804', '0.84876']
['0.83533', '0.82958', '0.80864', '0.84687']
['0.82648', '0.82219', '0.80731', '0.82806']
['0.81225', '0.80689', '0.77877', '0.85745']
['1.00000', '0.99862', '0.99084', '0.94839']
['0.99862', '1.00000', '0.99440', '0.94820']
['0.99084', '0.99440', '1.00000', '0.91968']
['0.94839', '0.94820', '0.91968', '1.00000']
[]
我只希望黄色突出显示值:
答案 0 :(得分:1)
这应该让你入门
filepath = 'D:\THESIS\DATA\outputData\covarince.txt'
field_offset = 5 # first element index to get
nb_lines = 3 # number of lines we want
line_index = 0 # current line
with open(filepath, 'r') as f:
for line in f:
line = line.strip()
if line == '' or line.startswith('#'):
# ignore comments and empty lines
continue
if line_index >= nb_lines:
# stop when we got all lines we want
break
# list of elements in line
elements = line.split()
print elements[line_index + field_offset]
line_index += 1