在我的任務中,我必須在兩個指定單詞(以這些單詞開頭的行)之間寫入一段文本到txt文件中。輸入文件看起來像這樣:將文本塊寫入文件
SP_LINE
texttexttexttext
texttexttexttext
texttexttexttext
texttexttexttext
texttexttexttext
texttexttexttext
//
NON_SP_LINE
texttexttexttext
texttexttexttext
texttexttexttext
texttexttexttext
texttexttexttext
texttexttexttext
END_NON_LINE
SP_LINE
texttexttexttext
texttexttexttext
texttexttexttext
texttexttexttext
texttexttexttext
texttexttexttext
//
我必須寫入文件SP_LINE之間的文本「//」 我試過這個:
import re
fr=open("various.multi").read()
gr=open("locus.txt",'w')
for m in re.finditer("SP_LINE",fr):
a=int(m.start())
for n in re.finditer("//", fr[a:]):
b=int(n.end())
gr.write(fr[a:b])
gr.close()
但事實並非如此工作