提取下一行文本fi

2024-10-03 21:35:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从.txt文件中提取特定信息。我已经想出了一种方法来隔离我需要的线条;但是,打印它们被证明是有问题的。在

with open('RCSV.txt','r') as RCSV:
   for line in RCSV.read().splitlines():
      if line.startswith('   THETA'):
           print(line.next())

当我使用line.next()时,会出现以下错误:

^{pr2}$

Here is a link to the .txt file
Here is a link to the area of the file in question

我要做的是提取出以θφ开头的那条线


Tags: 文件theto方法intxt信息here
3条回答

您可以使用next(input),作为:

with open('RCSV.txt', "r") as input:
    for line in input:
        if line.startswith('   THETA'):
           print(next(input), end='')
           break

你可以试试这个:

with open('RCSV.txt','r') as RCSV:
    for line in RCSV:
        if line.startswith('   THETA'):
            next_line = RCSV.readline() # or RCSV.next()
            print(next_line)

注意,在下一次迭代中,line将是next_line之后的一行。在

找到键后,可以使用标志获取所有行。在

例如:

with open('RCSV.txt','r') as RCSV:
    content = RCSV.readlines()
    flag = False                         #Check Flag
    for line in content:
        if not flag:
            if line.startswith('   THETA'):
                flag = True
        else:
            print(line)                  #Prints all lines after '   THETA'

或者如果你只需要下面的一行。在

^{pr2}$

相关问题 更多 >