【发布时间】:2014-11-06 02:22:49
【问题描述】:
我知道这里存在许多关于使用 python 2 查找和替换文件中的文本的问题。但是,作为 python 的新手,我不理解语法,可能目的也会有所不同。
我正在寻找一些非常简单的代码行,就像在 linux shellscript 中一样
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 我想使用同一个文件。没有不同的读写文件
-
这可以满足你的需要:stackoverflow.com/questions/5453267/…
标签: python-2.7