我需要在一个文件中搜索一些字符串,并将其中的内容复制到一个单独的文件中
例如,“Hello”在一个文件中重复多次。我想将这些“Hello”之间的文本复制到一个文件中。每次“Hello”重复后面的文本时,它都需要转到一个新文件
请确认我在下面的脚本中使用的逻辑是否正确,以及它给我的缩进错误从我的'如果'循环
import re
text_file = "Hello.txt"
search1 = "Hello"
outfile = "charlie"
write1 = False
x = 0
with open(text_file , "r") as infile:
fi = infile.readlines()
with open("outfile%s" % x, "w") as fo:
fo.write("============================================ \n")
for line in fi:
if search1 in line:
write1 = True
x +=1
fo.write(line)
elif write1:
fo.write(line)
试试这个
相关问题 更多 >
编程相关推荐