我知道这里存在很多关于使用python 2查找和替换文件中的文本的问题。然而,作为python的一个非常新的东西,我不理解语法并可能是目的也会有所不同。Python 2.x查找并替换多行文本
我在寻找的东西非常简单的代码行作为在Linux shell脚本
sed -i 's/find/replace/' *.txt
sed -i 's/find2/replace2/' *.txt
可以此代码的工作,以取代多行文本
with open('file.txt', 'w') as out_file:
out_file.write(replace_all('old text 1', 'new text 1'))
out_file.write(replace_all('old text 2', 'new text 2'))
而且,似乎有越来越另一换行符问题,我不想要。任何想法或帮助?
@ inspectorG4dget我想使用同一个文件。没有不同的读写文件 – gyeox29ns 2014-11-06 02:37:33
这就是你需要的:http://stackoverflow.com/questions/5453267/is-it-possible-to-modify-lines-in-a-file-in-place – user3885927 2014-11-06 04:59:51