我知道這裏存在很多關於使用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