我如何阅读特定行和该行下的 7 行

How can i read one specific line and 7 more lines under that one

我有这么大的 txt 文件

G29 2019 07 08 02 00 00 1.571122556925e-04-9.777068044059e-12 0.000000000000e+00
     6.100000000000e+01 6.809375000000e+01 3.670867192067e-09 2.120402980941e+00
     3.512948751450e-06 1.192553318106e-03 9.380280971527e-06 5.153736095428e+03
     9.360000000000e+04 4.656612873077e-08 1.565794672787e+00 8.195638656616e-08
     9.854740594187e-01 2.112812500000e+02 1.817507992857e+00-7.755323040334e-09
     2.692969315838e-10 1.000000000000e+00 2.061000000000e+03 0.000000000000e+00
     2.000000000000e+00 0.000000000000e+00-9.778887033463e-09 6.100000000000e+01
     9.999000000000e+08 0.000000000000e+00                                      
G30 2019 07 08 02 00 00-1.387689262629e-04-7.503331289627e-12 0.000000000000e+00
     4.300000000000e+01 3.143750000000e+01 5.236289541049e-09-1.114281617593e+00
     1.536682248116e-06 4.139962256886e-03 5.565583705902e-06 5.153780742645e+03
     9.360000000000e+04 3.166496753693e-08-5.518871386231e-01-4.097819328308e-08
     9.406574455640e-01 2.614687500000e+02-2.937938331210e+00-8.484639133562e-09
    -4.607334771129e-11 1.000000000000e+00 2.061000000000e+03 0.000000000000e+00
     2.000000000000e+00 0.000000000000e+00 3.725290298462e-09 4.300000000000e+01
     9.999000000000e+08 0.000000000000e+00                                      
J02 2019 07 08 01 00 00-9.192153811455e-07-3.410605131648e-13 0.000000000000e+00
     9.700000000000e+01-2.437500000000e+00 1.713285650939e-09-1.895301564152e+00
     1.378357410431e-07 7.528792973608e-02-1.499429345131e-06 6.493121416092e+03
     9.000000000000e+04 4.768371582031e-07-9.331615762110e-02-2.438202500343e-06
     7.607943375436e-01 2.135937500000e+02-1.583245983476e+00-1.648282943315e-09
     7.928901699153e-11 2.000000000000e+00 2.061000000000e+03 0.000000000000e+00
     2.900000000000e+00 0.000000000000e+00 1.396983861923e-09 8.650000000000e+02
     9.999000000000e+08 0.000000000000e+00    

并且我需要阅读以 G30 开头的所有行以及下面的所有行(在本示例中为 7 行),直到下一个片段开始。 我设法只读取了第一行和该行的编号,但无法添加其他行。

with open("filename", "r") as f:

    str1 = 'G30'
    for i,ln in enumerate(f):
        if  str1 in ln:
            print(ln[0:],i)

要将文件内容作为流处理,而不将整个内容读入内存,我认为您最好的选择是使用标志变量来决定是否是时候打印当前行。考虑这个算法:

  • 如果该行与起始模式匹配,则设置标志
  • 如果该行匹配结束模式,清除标志
  • 如果设置了标志,打印当前行

例如,像这样:

with open("filename") as fh:
    pattern = 'G30'
    should_print = False

    for line in fh:
        if line.startswith(pattern):
            should_print = True
        elif not line.startswith(' '):
            should_print = False

        if should_print:
            print(line, end='')

尝试:

fname = "testing.txt"
with open(fname) as f: 
    lines = f.readlines()
    listlen = len(lines)
    i = 0
    while i<listlen: 
        line = lines[i]
        if line.startswith("G30"): 
            print(line, end="")
            count =0
            while True: 
                i=i+1
                line = lines[i]
                print(line, end="")
                count=count+1
                if count>=7: break
        i=i+1

因为您知道 "G30" 开始的行的索引,所以只需选择接下来的 7 行。

with open("filename", "r") as f:
    str1 = 'G30'
    all_lines = f.readlines()
    for i,ln in enumerate(all_lines):
        if  str1 in ln:
            print(ln[0:])
            for l in range(1,8):
                print(all_lines[i+l])
while filename.readline()[:3] ==‘G30’:
    exit()

该行一开始程序就停止。

您可以创建一个单独的函数来打印所需的行数:

# this fn prints n number of lines
# it also removes printed lines from list
# and return remaining list: 
def printNlines(linelist, n):
    i=0
    while i<n: 
        if len(linelist)==0: break  # return if list becomes empty
        print(linelist[0], end="")  
        linelist = linelist[1:]     # remove printed line from list;
        i=i+1
    return linelist                 # return remaining list;

现在使用上面的函数浏览你的文件列表:

fname = "testing.txt"
with open(fname) as f: 
    lines = f.readlines()       # get list of lines from file;
while len(lines)>0:             # loop till the list is empty
    line = lines[0]             # read first line
    if line.startswith("G30"):  # test it
        lines = printNlines(lines, 1+7) # print current line and seven more; 
    else: lines = lines[1:]   # remove this line from list without printing;