我想循环一个文本文件的内容,并在一些行上进行搜索和替换,并将结果写回文件。我可以先把整个文件加载到内存中,然后再把它写回来,但这可能不是最好的方法。

在下面的代码中,做到这一点的最佳方法是什么?

f = open(file)
for line in f:
    if line.contains('foo'):
        newline = line.replace('foo', 'bar')
        # how to write this newline back to the file

当前回答

更python化的方法是使用上下文管理器,如下所示:

from tempfile import mkstemp
from shutil import move
from os import remove

def replace(source_file_path, pattern, substring):
    fh, target_file_path = mkstemp()
    with open(target_file_path, 'w') as target_file:
        with open(source_file_path, 'r') as source_file:
            for line in source_file:
                target_file.write(line.replace(pattern, substring))
    remove(source_file_path)
    move(target_file_path, source_file_path)

你可以在这里找到完整的片段。

其他回答

如果您删除缩进如下所示,它将在多行中搜索和替换。 请看下面的例子。

def replace(file, pattern, subst):
    #Create temp file
    fh, abs_path = mkstemp()
    print fh, abs_path
    new_file = open(abs_path,'w')
    old_file = open(file)
    for line in old_file:
        new_file.write(line.replace(pattern, subst))
    #close temp file
    new_file.close()
    close(fh)
    old_file.close()
    #Remove original file
    remove(file)
    #Move new file
    move(abs_path, file)

最短的方法可能是使用fileinput模块。例如,下面将行号添加到文件中,就地:

import fileinput

for line in fileinput.input("test.txt", inplace=True):
    print('{} {}'.format(fileinput.filelineno(), line), end='') # for Python 3
    # print "%d: %s" % (fileinput.filelineno(), line), # for Python 2

这里的情况是:

原始文件被移动到备份文件中 标准输出被重定向到循环中的原始文件 因此,任何打印语句都写回原始文件

Fileinput有更多附加功能。例如,它可以用来自动操作sys. exe中的所有文件。Args[1:],而不需要显式地迭代它们。从Python 3.2开始,它还为在with语句中使用提供了方便的上下文管理器。


虽然fileinput对于一次性脚本非常有用,但我在实际代码中使用它时会很谨慎,因为必须承认它不是很可读或熟悉。在实际的(生产)代码中,多写几行代码来明确过程,从而使代码可读是值得的。

有两种选择:

这个文件不是很大,您可以把它全部读入内存。然后关闭文件,以写入模式重新打开文件,并将修改后的内容写回。 文件太大,无法存储在内存中;您可以将其移动到一个临时文件并打开它,逐行读取它,然后将其写回原始文件。注意,这需要两倍的存储空间。

下面是另一个经过测试的例子,它将匹配搜索和替换模式:

import fileinput
import sys

def replaceAll(file,searchExp,replaceExp):
    for line in fileinput.input(file, inplace=1):
        if searchExp in line:
            line = line.replace(searchExp,replaceExp)
        sys.stdout.write(line)

使用示例:

replaceAll("/fooBar.txt","Hello\sWorld!$","Goodbye\sWorld.")

正如lassevk所建议的,在运行时写出新文件,下面是一些示例代码:

fin = open("a.txt")
fout = open("b.txt", "wt")
for line in fin:
    fout.write( line.replace('foo', 'bar') )
fin.close()
fout.close()

我想像这样的东西就可以了。它基本上将内容写入一个新文件,并用新文件替换旧文件:

from tempfile import mkstemp
from shutil import move, copymode
from os import fdopen, remove

def replace(file_path, pattern, subst):
    #Create temp file
    fh, abs_path = mkstemp()
    with fdopen(fh,'w') as new_file:
        with open(file_path) as old_file:
            for line in old_file:
                new_file.write(line.replace(pattern, subst))
    #Copy the file permissions from the old file to the new file
    copymode(file_path, abs_path)
    #Remove original file
    remove(file_path)
    #Move new file
    move(abs_path, file_path)