现在的位置: 首页 > 综合 > 正文

002_003 Python 搜索和替换文件中的文本

2018年02月15日 ⁄ 综合 ⁄ 共 266字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8

print '中国'

#搜索和替换文件中的文本
srcText = '123'
desText = '456'
input_file = open(r'D:\123.txt')
output_file = open(r'D:\1236.txt','w')
for s in input_file:
    output_file.write(s.replace(srcText,desText))
input_file.close()
output_file.close()

结果:

123中内容:123abc中国

1236中内容:456abc中国

抱歉!评论已关闭.