简体   繁体   English

查找行,然后获得下一行

[英]Find line, then get the next line

I have the following problem: I open a file and read it line by line searching for a specific pattern. 我有以下问题:打开文件并逐行阅读以搜索特定模式。 When I found it, I would like to write the entire line AND THE NEXT TWO LINES into a new file. 当我找到它时,我想将整行和接下来的两行写入一个新文件。 The problem is that I don't know how to get from the line that I've found to the next 2. 问题是我不知道如何从找到的那条线到下一条2。

AAA
XXX
XXX
BBB
XXX
XXX
CCC
XXX
XXX

In this example it would be that I find "BBB" and I want to get the next two lines. 在此示例中,我将找到“ BBB”,并且想要获得下两行。

What could I do? 我能做什么? Thank you very much for your kind help! 非常感谢您的帮助!

Edit: I realized that I have to ask more precisely. 编辑:我意识到我必须问得更准确。

This is the code from my colleague 这是我同事的代码

for k in range(0,len(watcrd)):
    if cvt[k]>cvmin:
        intwat+=1
        sumcv+=cvt[k]
        sumtrj+=trj[k]/((i+1)*sep/100)
        endline='%5.2f %5.2f' % (cvt[k],trj[k]/((i+1)*sep/100)) # ivan
        ftrj.write(watline[k][:55]+endline+'\n')
        fall.write(watline[k][:55]+endline+'\n')

For every k in range I would like to write k, k+1, k+2 to the file ftrj. 对于k in range每个k in range我想将k, k+1, k+2写入文件ftrj。 Which is the best way to do this? 哪个是最好的方法?

Edit 2: I am sorry, but I realized that I've made a mistake. 编辑2:对不起,但我意识到自己犯了一个错误。 What you suggested worked, but I realized that I have to include it in a different part of the code. 您的建议是可行的,但我意识到我必须将其包含在代码的不同部分中。

for line in lines[model[i]:model[i+1]]:
    if line.startswith('ATOM'):
    resi=line[22:26]
    resn=line[17:20]
    atn=line[12:16]
    crd=[float(line[31:38]),float(line[38:46]),float(line[46:54])]

    if (resn in noprot)==False and atn.strip().startswith('CA')==True:
        protcrd.append(crd)
    if (resn in reswat)==True and (atn.strip() in atwat)==True:
        watcrd.append(crd)
        watline.append(line)

I would think of something like this: 我会想到这样的事情:

    (...)
    if (resn in reswat)==True and (atn.strip() in atwat)==True:
        watcrd.append(crd)
        watline.append(line)
        for i in range(1, 3):
            try:
                watcrd.append(crd[line + i])
                watline.append(line[line + i])
            except IndexError:
                break

But it doesn't work. 但这是行不通的。 How can I indicate the part and the line that I want to append to this list? 如何指示要添加到此列表的零件和线?

Python file objects are iterators, you can always ask for the next lines: Python文件对象是迭代器,您总是可以要求下一行:

with open(inputfilename) as infh:
    for line in infh:
        if line.strip() == 'BBB':
            # Get next to lines:
            print next(infh)
            print next(infh)

Here next() function advances the infh iterator to the next line, returning that line. 在这里, next()函数infh迭代器前进到下一行,并返回该行。

However, you are not processing a file; 但是,您没有在处理文件。 you are processing a list instead; 您正在处理列表; you can always access later indices in the list: 您可以随时访问列表中的后续索引:

    ftrj.write(watline[k][:55]+endline+'\n')
    fall.write(watline[k][:55]+endline+'\n')
    for i in range(1, 3):
        try:
            ftrj.write(watline[k + i][:55]+endline+'\n')
            fall.write(watline[k + i][:55]+endline+'\n')
        except IndexError:
            # we ran out of lines in watline
            break

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

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