簡體   English   中英

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

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

我正在python3中編寫腳本,但無法解決以下問題。

我有使用此模式的名稱列表:

ZINC123456
ZINC234567
ZINC345678
ZINC456789
...

我有一個很大的文件,像這樣:

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

我想做的是:遍歷第一個列表中的每個項目,然后在第二個文件中搜索它。 找到此項目后,將以下行和所有以下內容復制到新文件中,直到到達下一個ZINCxxxxxx模式為止。

我怎樣才能做到這一點? 非常感謝您的幫助!

編輯:感謝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