简体   繁体   English

如何通过Python从表文本文件中获取价值?

[英]How to get value from table text file by Python?

i'm trying get value from table text file. 我正在尝试从表文本文件中获取价值。

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]

but result .... 但是结果....

['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']
[]

i want only value in yellow highlight: 我只想要黄色突出显示的值:

在此处输入图片说明

This should get you started 这应该让您开始

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM