我想在findall
找到一个搜索模式后,给文件添加一个新行。我使用的代码只将输入文件的内容写入输出文件。它不会向输出文件添加新行。如何修复代码?你知道吗
import re
text = """
Hi! How are you?
Can you hear me?
"""
with open("input.txt", "r") as infile:
readcontent = infile.readlines()
with open("output.txt", "w") as out_file:
for line in readcontent:
x1 = re.findall(text, line)
if line == x1:
line = line + text
out_file.write(line)
你知道吗输入.txt地址:
ricochet robots
settlers of catan
acquire
Hi! How are you?
Can you hear me?
this is very valuable
finish
想要的输出.txt地址:
ricochet robots
settlers of catan
acquire
Hi! How are you?
Can you hear me?
Added new line
this is very valuable
finish
尝试迭代每一行,检查文本是否存在。你知道吗
例如:
输出:
在这里不要使用
regex
。检查当前行,如果是要检查的行,则添加新行。你知道吗如果您需要一个
regex
本身,请选择以下内容(尽管我从不推荐):这就是你想要的:
output.txt
看起来像:相关问题 更多 >
编程相关推荐