代码如下:
#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中国