Я хотел бы добавить новую строку в файл после того, как 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)
input.txt:
ricochet robots
settlers of catan
acquire
Hi! How are you?
Can you hear me?
this is very valuable
finish
Желаемый файл output.txt:
ricochet robots
settlers of catan
acquire
Hi! How are you?
Can you hear me?
Added new line
this is very valuable
finish