这篇文章主要介绍了Python3搜索及替换文件中文本的方法,涉及Python操作文件及字符串的相关技巧,需要的朋友可以参考下
本文实例讲述了Python3搜索及替换文件中文本的方法。分享给大家供大家参考。具体实现方法如下:
# 将文件中的某个字符串改变成另一个 # 下面代码实现从一个特定文件或标准输入读取文件, # 然后替换字符串,然后写入一个指定的文件 import os, sys nargs = len(sys.argv) if not 3 <= nargs 3: input_file = open(sys.argv[3]) if nargs > 4: output_file = open(sys.argv[4], 'w') for s in input_file: output_file.write(s.replace(search_text, replace_text)) output_file.close() input_file.close()
希望本文所述对大家的Python程序设计有所帮助。
以上就是Py来源gao*daima.com搞@代#码网thon3搜索及替换文件中文本的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!