简体   繁体   English

用 Python 替换文件中的文本

[英]replacing text in a file with Python

I'm new to Python.我是 Python 的新手。 I want to be able to open a file and replace every instance of certain words with a given replacement via Python.我希望能够打开一个文件并通过 Python 用给定的替换替换某些单词的每个实例。 as an example say replace every word 'zero' with '0', 'temp' with 'bob', and say 'garbage' with 'nothing'.例如,将每个单词“零”替换为“0”,将“temp”替换为“bob”,将“垃圾”替换为“无”。

I had first started to use this:我首先开始使用这个:

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

but I don't think this is an even remotely correct way to do this.但我不认为这是一个甚至远程正确的方法来做到这一点。 I then thought about doing if statements to check if the line contains these items and if it does, then replace which one the line contains, but from what I know of Python this also isn't truly an ideal solution.然后我考虑使用 if 语句来检查该行是否包含这些项目,如果包含,则替换该行包含的项目,但根据我对 Python 的了解,这也不是真正的理想解决方案。 I would love to know what the best way to do this.我很想知道这样做的最佳方法是什么。 Thanks ahead of time!提前致谢!

This should do it这应该做

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.items():
            line = line.replace(src, target)
        outfile.write(line)

EDIT : To address Eildosa's comment , if you wanted to do this without writing to another file, then you'll end up having to read your entire source file into memory:编辑:为了解决Eildosa 的评论,如果您想在不写入另一个文件的情况下执行此操作,那么您最终将不得不将整个源文件读入内存:

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

Edit: If you are using Python 2.x, use replacements.iteritems() instead of replacements.items()编辑:如果您使用的是 Python 2.x,请使用replacements.iteritems()而不是replacements.items()

If your file is short (or even not extremely long), you can use the following snippet to replace text in place:如果您的文件很短(甚至不是很长),您可以使用以下代码段替换文本:

# 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'))

I might consider using a dict and re.sub for something like this:我可能会考虑使用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))

This has a slight advantage to replace in that it is a bit more robust to overlapping matches.这有一个微弱的优势replace的,因为它是更稳健的重叠匹配了一下。

The essential way is最基本的方法是

  • read() , read()
  • data = data.replace() as often as you need and then data = data.replace()根据您的需要,然后
  • write() . write()

If you read and write the whole data at once or in smaller parts is up to you.如果您一次读取和写入整个数据或以较小的部分读取和写入,则取决于您。 You should make it depend on the expected file size.您应该使其取决于预期的文件大小。

read() can be replaced with the iteration over the file object. read()可以替换为对文件对象的迭代。

Faster way of writing it would be...更快的写作方式是......

in = open('path/to/input/file').read()
out = open('path/to/input/file', 'w')
replacements = {'zero':'0', 'temp':'bob', 'garbage':'nothing'}
for i in replacements.keys():
    in = in.replace(i, replacements[i])
out.write(in)
out.close

This eliminated a lot of the iterations that the other answers suggest, and will speed up the process for longer files.这消除了其他答案建议的许多迭代,并将加快较长文件的过程。

Reading from standard input, write 'code.py' as follows:从标准输入读取,编写“code.py”如下:

import sys

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

for line in sys.stdin:
    for k, v in rep.iteritems():
        line = line.replace(k, v)
    print line

Then, execute the script with redirection or piping ( http://en.wikipedia.org/wiki/Redirection_(computing) )然后,使用重定向或管道执行脚本( http://en.wikipedia.org/wiki/Redirection_(computing)

python code.py < infile > outfile

This is a short and simple example I just used:这是我刚刚使用的一个简短而简单的示例:

If:如果:

fp = open("file.txt", "w")

Then:然后:

fp.write(line.replace('is', 'now'))
// "This is me" becomes "This now me"

Not:不是:

line.replace('is', 'now')
fp.write(line)
// "This is me" not changed while writing

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

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