问题:我有一个.txt文件,其中的字符串如下:
Q. Going back to July who all lived with you? 9 A. Ten people. 10 Q. Okay. So for the year, you had ten people living with you? 13 A. Yes.
Q. Going back to July who all lived with you? 9 A. Ten people. 10 Q. Okay. So for the year, you had ten people living with you? 13 A. Yes.
Q. Going back to July who all lived with you? 9 A. Ten people. 10 Q. Okay. So for the year, you had ten people living with you? 13 A. Yes.
我想做的事:我希望循环浏览整个.txt文件,并在每个"Q"前面添加一个"\n"one_answers"A."这样,当我重新打开.txt文件时,每个Q.和A.字符串都在一个单独的行上。
您可以使用.replace()
将每个"Q."
替换为"nQ."
(类似于"A."
(。
with open("in.txt") as in_file, open("out.txt", "w") as out_file:
for line in in_file:
out_file.write(line.replace("A.", "nA.").replace("Q.", "nQ."))