rep_word = ‘The piece is gone, left the puzzle undone‘
# \ 换行,跟shell一样
with open(‘nothing‘, ‘r‘, encoding=‘utf-8‘) as f, open(‘nothing1‘, ‘w‘, encoding=‘utf-8‘) as f_new:
for line in f:
if rep_word in line:
line = line.replace(rep_word, ‘碎片散去,留下已解的迷‘)
f_new.write(line)
# 运行前 nothing
# Trying hard to fill the emptiness
# The piece is gone, left the puzzle undone
# Ain‘t that the way it is
# 运行后 nothing1
# Trying hard to fill the emptiness
# 碎片散去,留下已解的迷
# Ain‘t that the way it is
原文:https://www.cnblogs.com/PAYNE1Z/p/8966354.html