292

如何使用 Python 3 搜索和替换文件中的文本?

这是我的代码:

import os
import sys
import fileinput

print ("Text to search for:")
textToSearch = input( "> " )

print ("Text to replace it with:")
textToReplace = input( "> " )

print ("File to perform Search-Replace on:")
fileToSearch  = input( "> " )
#fileToSearch = 'D:\dummy1.txt'

tempFile = open( fileToSearch, 'r+' )

for line in fileinput.input( fileToSearch ):
    if textToSearch in line :
        print('Match Found')
    else:
        print('Match Not Found!!')
    tempFile.write( line.replace( textToSearch, textToReplace ) )
tempFile.close()


input( '\n\n Press Enter to exit...' )

输入文件:

hi this is abcd hi this is abcd
This is dummy text file.
This is how search and replace works abcd

当我在上面的输入文件中搜索并用“abcd”替换“ram”时,它就像一个魅力。但是当我反之亦然,即用“ram”替换“abcd”时,最后会留下一些垃圾字符。

用“ram”替换“abcd”

hi this is ram hi this is ram
This is dummy text file.
This is how search and replace works rambcd
4

20 回答 20

456

正如 michaelb958 所指出的,您不能用不同长度的数据替换原位,因为这会使其余部分不合适。我不同意其他海报建议您从一个文件读取并写入另一个文件。相反,我会将文件读入内存,修复数据,然后在单独的步骤中将其写入同一个文件。

# Read in the file
with open('file.txt', 'r') as file :
  filedata = file.read()

# Replace the target string
filedata = filedata.replace('ram', 'abcd')

# Write the file out again
with open('file.txt', 'w') as file:
  file.write(filedata)

除非您要处理的文件太大而无法一次性加载到内存中,或者您担心如果在将数据写入文件的第二步期间进程中断,则可能会丢失数据。

于 2013-06-17T06:29:50.737 回答
316

fileinput已经支持就地编辑。在这种情况下,它会重定向stdout到文件:

#!/usr/bin/env python3
import fileinput

with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(text_to_search, replacement_text), end='')
于 2013-12-15T10:47:01.250 回答
59

正如 Jack Aidley 发布的和 JF Sebastian 指出的那样,这段代码不起作用:

 # Read in the file
filedata = None
with file = open('file.txt', 'r') :
  filedata = file.read()

# Replace the target string
filedata.replace('ram', 'abcd')

# Write the file out again
with file = open('file.txt', 'w') :
  file.write(filedata)`

但是这段代码会起作用(我已经测试过了):

f = open(filein,'r')
filedata = f.read()
f.close()

newdata = filedata.replace("old data","new data")

f = open(fileout,'w')
f.write(newdata)
f.close()

使用这种方法,filein 和 fileout 可以是同一个文件,因为 Python 3.3 会在打开写入时覆盖文件。

于 2014-04-05T05:19:15.027 回答
53

您可以像这样进行替换

f1 = open('file1.txt', 'r')
f2 = open('file2.txt', 'w')
for line in f1:
    f2.write(line.replace('old_text', 'new_text'))
f1.close()
f2.close()
于 2013-06-17T05:32:29.073 回答
22

您也可以使用pathlib.

from pathlib2 import Path
path = Path(file_to_search)
text = path.read_text()
text = text.replace(text_to_search, replacement_text)
path.write_text(text)
于 2019-02-08T02:24:59.110 回答
11

(点安装python-util)

from pyutil import filereplace

filereplace("somefile.txt","abcd","ram")

将所有出现的“abcd”替换为“ram”。
该函数还通过指定支持正则表达式regex=True

from pyutil import filereplace

filereplace("somefile.txt","\\w+","ram",regex=True)

免责声明:我是作者(https://github.com/MisterL2/python-util

于 2019-08-23T13:43:29.623 回答
5

迟到的答案,但这是我用来在文本文件中查找和替换的内容:

with open("test.txt") as r:
  text = r.read().replace("THIS", "THAT")
with open("test.txt", "w") as w:
  w.write(text)

演示

于 2020-08-22T03:35:15.270 回答
5

这个答案对我有用。以读取模式打开文件。以字符串格式读取文件。按预期替换文本。关闭文件。再次以写入模式打开文件。最后,将替换后的文本写入同一个文件。

    with open("file_name", "r+") as text_file:
        texts = text_file.read()
        texts = texts.replace("to_replace", "replace_string")
    with open(file_name, "w") as text_file:
        text_file.write(texts)
except FileNotFoundError as f:
    print("Could not find the file you are trying to read.")
于 2021-04-16T17:40:43.540 回答
3

使用单个 with 块,您可以搜索和替换您的文本:

with open('file.txt','r+') as f:
    filedata = f.read()
    filedata = filedata.replace('abc','xyz')
    f.truncate(0)
    f.write(filedata)
于 2019-06-14T13:00:46.580 回答
2

您的问题源于读取和写入同一个文件。而不是打开fileToSearch写入,打开一个实际的临时文件,然后在完成并关闭后tempFile,使用os.rename移动新文件fileToSearch

于 2013-06-17T05:43:36.550 回答
2

我的变体,在整个文件中一次一个字。

我把它读进了内存。

def replace_word(infile,old_word,new_word):
    if not os.path.isfile(infile):
        print ("Error on replace_word, not a regular file: "+infile)
        sys.exit(1)

    f1=open(infile,'r').read()
    f2=open(infile,'w')
    m=f1.replace(old_word,new_word)
    f2.write(m)
于 2013-12-15T10:19:22.163 回答
1

我遇到了同样的问题。问题是,当您在变量中加载 .txt 时,您将其用作字符串数组,而它是字符数组。

swapString = []
with open(filepath) as f: 
    s = f.read()
for each in s:
    swapString.append(str(each).replace('this','that'))
s = swapString
print(s)

于 2021-02-26T13:07:27.650 回答
1

我试过这个并使用 readlines 而不是 read

with open('dummy.txt','r') as file:
    list = file.readlines()
print(f'before removal {list}')
for i in list[:]:
        list.remove(i)

print(f'After removal {list}')
with open('dummy.txt','w+') as f:
    for i in list:
        f.write(i)
于 2021-03-02T10:44:58.530 回答
0

我已经这样做了:

#!/usr/bin/env python3

import fileinput
import os

Dir = input ("Source directory: ")
os.chdir(Dir)

Filelist = os.listdir()
print('File list: ',Filelist)

NomeFile = input ("Insert file name: ")

CarOr = input ("Text to search: ")

CarNew = input ("New text: ")

with fileinput.FileInput(NomeFile, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(CarOr, CarNew), end='')

file.close ()
于 2017-02-16T21:26:18.753 回答
0

我稍微修改了 Jayram Singh 的帖子,以替换“!”的每个实例 字符到我想随着每个实例递增的数字。认为这对于想要修改每行出现多次并想要迭代的角色可能会有所帮助。希望对某人有所帮助。PS-如果我的帖子以任何方式不合适,我对编码很抱歉,但这对我有用。

f1 = open('file1.txt', 'r')
f2 = open('file2.txt', 'w')
n = 1  

# if word=='!'replace w/ [n] & increment n; else append same word to     
# file2

for line in f1:
    for word in line:
        if word == '!':
            f2.write(word.replace('!', f'[{n}]'))
            n += 1
        else:
            f2.write(word)
f1.close()
f2.close()
于 2017-09-24T16:57:20.813 回答
0
def word_replace(filename,old,new):
    c=0
    with open(filename,'r+',encoding ='utf-8') as f:
        a=f.read()
        b=a.split()
        for i in range(0,len(b)):
            if b[i]==old:
                c=c+1
        old=old.center(len(old)+2)
        new=new.center(len(new)+2)
        d=a.replace(old,new,c)
        f.truncate(0)
        f.seek(0)
        f.write(d)
    print('All words have been replaced!!!')
于 2018-01-23T18:45:54.890 回答
0

除了已经提到的答案之外,这里解释了为什么最后会有一些随机字符:您正在以模式而不是模式
打开文件。主要区别在于该模式会在您打开文件后立即清除文件的内容,而不会。 这意味着如果您的文件内容是“123456789”并且您在其中写入“www”,则会得到“www456789”。它用新输入覆盖字符,但保留任何剩余输入不变。 您可以使用 清除文件内容的一部分,但最好先将更新的文件内容保存为字符串,然后一次执行并写入。 或者你可以使用我的图书馆:Dr+wwr+

truncate(<startPosition>)truncate(0)

于 2020-12-15T01:18:22.917 回答
-2
def findReplace(find, replace):

    import os 

    src = os.path.join(os.getcwd(), os.pardir) 

    for path, dirs, files in os.walk(os.path.abspath(src)):

        for name in files: 

            if name.endswith('.py'): 

                filepath = os.path.join(path, name)

                with open(filepath) as f: 

                    s = f.read()

                s = s.replace(find, replace) 

                with open(filepath, "w") as f:

                    f.write(s) 
于 2018-06-20T10:06:18.560 回答
-2

像这样:

def find_and_replace(file, word, replacement):
  with open(file, 'r+') as f:
    text = f.read()
    f.write(text.replace(word, replacement))
于 2020-01-17T07:08:12.140 回答
-2

我已经把它作为一门课程的练习来解决:打开文件,查找和替换字符串并写入一个新文件。

class Letter:

    def __init__(self):

        with open("./Input/Names/invited_names.txt", "r") as file:
            # read the list of names
            list_names = [line.rstrip() for line in file]
            with open("./Input/Letters/starting_letter.docx", "r") as f:
                # read letter
                file_source = f.read()
            for name in list_names:
                with open(f"./Output/ReadyToSend/LetterTo{name}.docx", "w") as f:
                    # replace [name] with name of the list in the file
                    replace_string = file_source.replace('[name]', name)
                    # write to a new file
                    f.write(replace_string)


brief = Letter()
于 2021-02-15T16:52:31.567 回答