Python3 替换文件中的字符串
2018-11-19 本文已影响0人
ElonYanJ
def repalcebytext(address, ori_str, target_str):
all_data = ''
with open(address, 'r+', encoding="UTF-8") as f:
for line in f.readlines():
if line.find(ori_str) != -1:
line = target_str
all_data += line
f.close()
with open(address, 'r+', encoding="UTF-8") as f:
f.truncate()
f.write(all_data)
f.close()