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

这是我的代码:

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

当我在上面的输入文件中搜索并将“ram”替换为“abcd”时,它就像一个咒语。但当我反过来做,即替换'abcd'由'ram',一些垃圾字符被留在最后。

将'abcd'替换为'ram'

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

当前回答

您的问题源于读取和写入同一个文件。与其打开fileToSearch进行写入,不如打开一个实际的临时文件,然后在您完成并关闭tempFile之后,使用os。rename将新文件移动到fileToSearch上。

其他回答

使用re.subn可以对替换过程进行更多的控制,例如将单词分成两行,区分大小写的匹配。此外,它返回匹配的数量,如果没有找到字符串,可以使用这些匹配来避免浪费资源。

import re

file = # path to file

# they can be also raw string and regex
textToSearch = r'Ha.*O' # here an example with a regex
textToReplace = 'hallo'

# read and replace
with open(file, 'r') as fd:
    # sample case-insensitive find-and-replace
    text, counter = re.subn(textToSearch, textToReplace, fd.read(), re.I)

# check if there is at least a  match
if counter > 0:
    # edit the file
    with open(file, 'w') as fd:
        fd.write(text)

# summary result
print(f'{counter} occurence of "{textToSearch}" were replaced with "{textToReplace}".')

一些正则表达式:

添加re.I标志,re.IGNORECASE的缩写形式,用于不区分大小写的匹配 对于多行替换re.subn(r'\n*'.join(textToSearch), textToReplace, fd.read())),取决于数据也'\n{,1}'。注意,在这种情况下,textToSearch必须是纯字符串,而不是正则表达式!

像这样:

def find_and_replace(file, word, replacement):
  with open(file, 'r+') as f:
    text = f.read()
    f.write(text.replace(word, replacement))

你也可以使用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)

我稍微修改了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()

(pip install python-util)

from pyutil import filereplace

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

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

from pyutil import filereplace

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

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