简体   繁体   English

从文件导入特定文本以在 Python 中列出

[英]Import specific text from a file to list in Python

I have a text file which looks like:我有一个文本文件,它看起来像:


line A
line B
line C
line D

objective
8.5770822e+000
3762931e+000
0996787e+000
0070925e+000
0003053e+000
9999994e+000

line E
line F
line G

I want to import numbers under the word "objective" into a Python list.我想将“目标”一词下的数字导入 Python 列表。

Challenges: The number of lines (with numbers) under the line with "objective" need not be the same.挑战:“目标”行下的行数(带数字)不必相同。 It varies from file to file.它因文件而异。 I am trying to develop a generic code.我正在尝试开发通用代码。 So the stop condition must be based on a blank line under the numbers.所以停止条件必须基于数字下的空行。

My idea: I was able to access the line "objective" with the code if line.startswith("objective") But to access the lines below until you find a blank line seems challenging.我的想法: if line.startswith("objective")我可以使用代码访问if line.startswith("objective")行但是要访问下面的行直到找到一个空行似乎很有挑战性。

Can you please help me with this?你能帮我解决这个问题吗?

Thank you very much.非常感谢。

One way to do this would be to have a flag indicating whether or not the word "objective" has been seen.这样做的一种方法是设置一个标志,指示是否已看到“目标”一词。

Something like this:像这样的东西:

res = []
objective_seen = False
with open(filename) as fh:
    for line in fh:
        if objective_seen:
            if line:
                res.append(line)
            else:  # blank line
                break
        else:  # objective not yet seen
            if line.startswith('objective'):
                objective_seen = True

Another way would be to have two loops, one to process the file up to "objective", the other to process the rest:另一种方法是有两个循环,一个将文件处理到“目标”,另一个处理其余部分:

res = []
with open(filename) as fh:
    for line in fh:
        if line.startswith('objective'):
            break
    else:
        raise ValueError('No "objective" section in file')

    for line in fh:
        if line:
            res.append(line)
        else:
            break

Here is my proposition:这是我的提议:

  • Read the file into a list将文件读入列表
  • save each lines from "Objective" to the last line into a new list numbers将“目标”到最后一行的每一行保存到一个新的列表numbers
  • take away the 0th elements and all the elements from the first empty '' elements to the end. 0th元素和从第一个空''元素到末尾的所有元素。

What will be left are the numbers you need.剩下的就是你需要的数字。

Here is my implem这是我的工具

def main():
    #read file 
    g = open(r"test.txt")
    # read all lines
    lst= g.readlines();
    numbers = []
    # at the beginning no number is found

    # flag to track if we shall save or not in the list
    start_saving = False
    # a loop to save all from objective to the end
    for l in lst:
       l=l.strip("\n")
       if ("objective" in l):
            start_saving= True
       if (start_saving):
            numbers.append(l)

    # here we  obtain an array containing all elements from Objective to the end
    # ['objective', '8.5770822e+000', '3762931e+000', '0996787e+000', '0070925e+000', '0003053e+000', '9999994e+000', '', 'line E', 'line F', 'line G']
    # i will now strip away all elements of index 0 ("Objective") and from the first empty index (the first line containing nothing)
    # only keep elements from 1st to the first element not empty
    numbers = numbers[1:numbers.index('')]
    print numbers
    # display : ['8.5770822e+000', '3762931e+000', '0996787e+000', '0070925e+000', '0003053e+000', '9999994e+000']

if __name__ == '__main__':
    main()

This outputs:这输出:

['8.5770822e+000', '3762931e+000', '0996787e+000', '0070925e+000', '0003053e+000', '9999994e+000']

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

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