简体   繁体   English

搜索文件中的字符串,并复制后面的所有行,直到string2

[英]Search file for string and copy all lines following until string2

I am writing a script in python3 but I can't solve the following problem. 我正在python3中编写脚本,但无法解决以下问题。

I have a list of names with this pattern: 我有使用此模式的名称列表:

ZINC123456
ZINC234567
ZINC345678
ZINC456789
...

and I have a big file like this: 我有一个很大的文件,像这样:

ZINC123456
xxx
xxx
xxx
ZINC987654
xxy
xxy
xxy
xxy
ZINC654987
...

What I want to do is: Loop over every item in the first list and search it in the second file. 我想做的是:遍历第一个列表中的每个项目,然后在第二个文件中搜索它。 When this item is found copy this line and all the following until the next ZINCxxxxxx pattern is reached into a new file. 找到此项目后,将以下行和所有以下内容复制到新文件中,直到到达下一个ZINCxxxxxx模式为止。

How can I do this? 我怎样才能做到这一点? Thank you very much for your help! 非常感谢您的帮助!

EDIT: Thanks to Sudipta Chatterjee I found the following solution: 编辑:感谢Sudipta Chatterjee,我找到了以下解决方案:

import sys
finZ=open(sys.argv[1],'r')
finX=open('zinc.sdf','r')
fout=open(sys.argv[1][:7]+'.sdf','w')

list=[]
thislinehaszinc = False
zincmatching    = False

for zline in finZ:
if zline[0:4] == "ZINC":
    name = zline[:-1] #line[4:-1]
    if name not in list:
        list.append(name)

for xline in finX:
if xline[0:4] == "ZINC":
    thislinehaszinc = True
    zincmatching    = False
    for line in list:
        if line == xline[:-1]:
            zincmatching    = True
            fout.write(xline)
            print('Found: '+xline)
            pass
        else:
            pass
else:
    thislinehaszinc = False

if thislinehaszinc == False and zincmatching == True:
    fout.write(xline)
# Clarified from comments - the program is to act as a filter so that any lines
# which have a pattern 'ZINC' in the second file but do not belong in the first
# should stop the dump until the next matching zinc is found

fileZ = open ('file_with_zinc_only.txt', 'r').readlines()
fileX = open ('file_with_x_info.txt', 'r').readlines()
fileOutput = open ('file_for_output.txt', 'w')

thisLineHasZinc = False
zincMatching = False

for xline in fileX:
    #print "Dealing with", xline
    if len(xline.split('ZINC')) != 1:
        thisLineHasZinc = True
        zincMatching = False
        for zline in fileZ:
            #print "Trying to match",zline
            if zline == xline:
                #print "************MATCH***************"
                zincMatching = True
                fileOutput.write (zline)
                #print "**",xline
                break
    else:    
        thisLineHasZinc = False

    # If we are currently under a block where we've found a ZINC previously
    # but not yet reached another ZINC line, write to file
    #print 'thisLineHasZinc',thisLineHasZinc,'zincMatching',zincMatching
    if thisLineHasZinc == False and zincMatching == True:
        fileOutput.write (xline)
        #print "**** "+ xline

fileOutput.close()

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

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