提取文本文件的下一行 [英] Extracting next line of text file

查看:95
本文介绍了提取文本文件的下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从.txt文件中提取特定信息.我想出了一种隔离所需线路的方法.但是,事实证明,打印它们是有问题的.

I am trying to extract specific information from a .txt file. I have figured out a way to isolate the lines I need; however, printing them has proven to be problematic.

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

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

When I use line.next() it gives me this error:

"AttributeError: 'str' object has no attribute 'next'"

此处是指向.txt文件的链接
此处是指向相关文件区域的链接

我想做的是提取以'THETA PHI'等开头的行之后的行.

What I'm trying to do is extract the line following the line that starts with 'THETA PHI' etc.

推荐答案

找到密钥后,可以使用标志获取所有行.

You can use a flag to get all lines after you find the key.

例如:

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'

或者如果您只需要以下一行.

Or if you need just the following line.

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

这篇关于提取文本文件的下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆