002_003 Pythonファイルのテキストの検索と置換

405 ワード

コードは次のとおりです.
#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中内容:123 abc中国
1236中内容:456 abc中国