我有一本txt格式的书。我想创建2个新文本:在第一个中,我想用"Paul"
替换字符串Paul_1
的所有出现,在第二个用Paul_2
。
我写了这段代码:
with open("book.txt", 'r') as original, \
open("book_1.txt", 'w') as mod1, \
open("book_2.txt", 'w') as mod2:
for line in original:
words = line.split()
for word in words:
s="Paul"
if(word == s):
mod1.write(word + "_1 ")
mod2.write(word + "_2 ")
else:
mod1.write(word + " ")
mod2.write(word + " ")
mod1.write("\n")
mod2.write("\n")
存在一个问题,通常会跳过一些Paul
因此,最后,我在同一文档中同时包含Paul
和Paul_1
(以及Paul
和Paul_2
)。问题在哪里?
答案 0 :(得分:2)
这应该有所帮助。
import re
with open("book.txt", 'r') as original, \
open("book_1.txt", 'w') as mod1, \
open("book_2.txt", 'w') as mod2:
data = original.read()
data_1 = re.sub(r"\bPaul\b", 'Paul_1', data) #Replace any occurrence of Paul with Paul_1
data_2 = re.sub(r"\bPaul\b", 'Paul_2', data) #Replace any occurrence of Paul with Paul_2
mod1.write(data_1 + r"\n")
mod2.write(data_2 + r"\n")