用Python替换文件中的文本

sha*_*nar 25 python string replace file

我是Python的新手.我希望能够打开一个文件,并通过Python替换给定替换的某些单词的每个实例.例如,用'bo'替换每个单词'zero',用'bob'替换'temp',用'nothing'说'trash'.

我第一次开始使用它:

for line in fileinput.input(fin):
        fout.write(line.replace('zero', '0'))
        fout.write(line.replace('temp','bob'))
        fout.write(line.replace('garbage','nothing'))
Run Code Online (Sandbox Code Playgroud)

但我不认为这是一种甚至是远程正确的方法.然后我考虑使用if语句来检查行是否包含这些项,如果它包含这些项,然后替换该行包含的那个,但是根据我所知的Python,这也不是真正理想的解决方案.我很想知道最好的方法是什么.提前谢谢!

ins*_*get 72

这应该做到这一点

replacements = {'zero':'0', 'temp':'bob', 'garbage':'nothing'}

with open('path/to/input/file') as infile, open('path/to/output/file', 'w') as outfile:
    for line in infile:
        for src, target in replacements.iteritems():
            line = line.replace(src, target)
        outfile.write(line)
Run Code Online (Sandbox Code Playgroud)

编辑:要解决Eildosa的评论,如果你想这样做而不写另一个文件,那么你最终必须将整个源文件读入内存:

lines = []
with open('path/to/input/file') as infile:
    for line in infile:
        for src, target in replacements.iteritems():
            line = line.replace(src, target)
        lines.append(line)
with open('path/to/input/file', 'w') as outfile:
    for line in lines:
        outfile.write(line)
Run Code Online (Sandbox Code Playgroud)

编辑:如果您使用的是Python 3.x,请使用replacements.items()而不是replacements.iteritems()

  • 这个解决方案真的有效吗?当调用`outfile = open('path/to/input/file','w')时,文件会立即被覆盖`````总是为空 (2认同)

mgi*_*son 7

我可能会考虑使用a dictre.sub类似的东西:

import re
repldict = {'zero':'0', 'one':'1' ,'temp':'bob','garage':'nothing'}
def replfunc(match):
    return repldict[match.group(0)]

regex = re.compile('|'.join(re.escape(x) for x in repldict))
with open('file.txt') as fin, open('fout.txt','w') as fout:
    for line in fin:
        fout.write(regex.sub(replfunc,line))
Run Code Online (Sandbox Code Playgroud)

这有一点点优势replace,因为它对重叠匹配更加健壮.

  • @ inspectorG4dget - 如果有重叠匹配,则有必要.(`line.replace('bob','robert').replace('robert','foo')`)将`bob`改为`foo`,这可能不是很理想,但是你可以用`re`来避免它.此外,由于它全部在1中完成,它可能更有效(对于小文件不太重要,但对于大文件很重要). (3认同)

Joh*_*ote 7

如果您的文件很短(甚至不是很长),您可以使用以下代码段来替换文本:

# Replace variables in file
with open('path/to/in-out-file', 'r+') as f:
    content = f.read()
    f.seek(0)
    f.truncate()
    f.write(content.replace('replace this', 'with this'))
Run Code Online (Sandbox Code Playgroud)


glg*_*lgl 5

最基本的方法是

  • read(),
  • data = data.replace() 只要你需要,然后
  • write().

如果您一次读取和写入整个数据或以较小的部分读取和写入,则取决于您。您应该使其取决于预期的文件大小。

read() 可以替换为对文件对象的迭代。