替换文件内容中的字符串 [英] Replace string within file contents
本文介绍了替换文件内容中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何打开一个文件Stud.txt,然后用Orange替换出现的A?
解决方案
开放(Stud.txt,rt)作为fin:
开放(out.txt,wt)作为fout:$ b
$ b $
$ f $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $How can I open a file, Stud.txt, and then replace any occurences of "A" with "Orange"?
解决方案 with open("Stud.txt", "rt") as fin:
with open("out.txt", "wt") as fout:
for line in fin:
fout.write(line.replace('A', 'Orange'))
这篇关于替换文件内容中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文