简体   繁体   English

python替换文本文件中的单词而无需逐行

[英]python replace word in text file without going line by line

I have a text file, say: 我有一个文本文件,说:

This is a text document
written in notepad

I want to replace 'document' with the word 'file' and 'notepad' with the word 'Notepad' and then I want to save / override the file. 我想用单词“文件”代替“文档”,用单词“记事本”代替“记事本”,然后我要保存/覆盖文件。 Now, without going line by line, because I know I can do 现在,无需逐行进行,因为我知道我可以做到

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()

but is there a way to do it without going line by line? 但是有没有一种方法可以不用逐行进行呢? Note: I am using python 2.7. 注意:我使用的是python 2.7。

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

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

Quoting from the docs , 引用文档

For reading lines from a file, you can loop over the file object. 要从文件中读取行,可以在文件对象上循环。 This is memory efficient, fast, and leads to simple code 这是高效,快速的内存,并导致简单的代码

So, I were you, I would have done it like this 所以,我就是你,我会这样做的

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")

If you prefer one-liners, you replace the with part with this 如果您更喜欢单线,则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))

If memory is not a problem and you still want not to iterate over the file object, you can have the contents of the entire file moved to memory and then replace it there 如果内存不是问题,并且您仍然不想遍历文件对象,则可以将整个文件的内容移到内存中,然后在其中替换

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)

With similar code,it's also possible to use re.sub method available in the re module to substitute based on the regular expression. 使用类似的代码,还可以使用re模块中可用的re.sub方法来基于正则表达式进行替换。 However, using this method would require traversing the file content N times if you need to replace N patterns. 但是,如果您需要替换N个模式,则使用此方法将需要遍历N次文件内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM