python替换文本文件中的单词而不是逐行

时间:2014-01-26 23:59:42

标签: python python-2.7 text-files

我有一个文本文件,说:

This is a text document
written in notepad

我想用“记事本”这个词替换“文件”和“记事本”,然后我想保存/覆盖文件。现在,没有逐行,因为我知道我可以做到

wordReplacements = {'document':'file', 'notepad':'Notepad'}
contents = open(filePath, 'r')
for line in contents:
    for key, value in wordReplacements.iteritems():
        line = line.replace(key, value)
contents.close()

但有没有办法在不逐行的情况下完成? 注意:我使用的是python 2.7。

3 个答案:

答案 0 :(得分:2)

with open(sys.argv[1]) as f:
  words = f.read().replace("foo", "bar")

with open(sys.argv[1], "wb") as f:
  f.write(words)

答案 1 :(得分:2)

引自docs

  

要从文件中读取行,可以循环遍历文件对象。这个   内存高效,快速,并导致简单的代码

所以,我是你,我会这样做的

import os
wordReplacements = {'document':'file', 'notepad':'Notepad'}

def transform_line(line):
    for key, value in wordReplacements.iteritems():
        line = line.replace(key, value)
    return line

with open("Output.txt", "w") as output_file, open("Input.txt") as input_file:
    for line in input_file:
        output_file.write(transform_line(line))

os.rename("Output.txt", "Input.txt")

如果您更喜欢单行,则用此

替换with部分
with open("Output.txt", "w") as output_file, open("Input.txt") as input_file:
    output_file.write("".join(transform_line(line) for line in input_file))

如果内存不是问题并且您仍然希望不迭代文件对象,则可以将整个文件的内容移动到内存中,然后将其替换为

import re
with open("Input.txt") as open_file:
    data = open_file.read()
for key, value in wordReplacements.iteritems():
    data = re.sub(key, value, data)
with open("Input.txt", "wb") as open_file:
    open_file.write(data)

答案 2 :(得分:0)

使用类似的代码,也可以使用re模块中可用的re.sub方法替换基于正则表达式。但是,如果需要替换N个模式,使用此方法将需要遍历文件内容N次。