Python 如何搜索和替换文件中的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17140886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search and replace text in a file?
提问by Shriram
How do I search and replace text in a file using Python 3?
如何使用 Python 3 搜索和替换文件中的文本?
Here is my code:
这是我的代码:
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...' )
Input file:
输入文件:
hi this is abcd hi this is abcd
This is dummy text file.
This is how search and replace works abcd
When I search and replace 'ram' by 'abcd' in above input file, it works as a charm. But when I do it vice-versa i.e. replacing 'abcd' by 'ram', some junk characters are left at the end.
当我在上面的输入文件中用 'abcd' 搜索和替换 'ram' 时,它就像一个魅力。但是当我反之亦然,即用“ram”替换“abcd”时,最后会留下一些垃圾字符。
Replacing 'abcd' by 'ram'
用 'ram' 替换 'abcd'
hi this is ram hi this is ram
This is dummy text file.
This is how search and replace works rambcd
采纳答案by jfs
fileinputalready supports inplace editing. It redirects stdoutto the file in this case:
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='')
回答by Jayram
You can do the replacement like this
你可以像这样进行更换
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()
回答by icktoofay
Your problem stems from reading from and writing to the same file. Rather than opening fileToSearchfor writing, open an actual temporary file and then after you're done and have closed tempFile, use os.renameto move the new file over fileToSearch.
您的问题源于读取和写入同一个文件。而不是打开fileToSearch写入,打开一个实际的临时文件,然后在你完成并关闭后tempFile,使用os.rename将新文件移动到fileToSearch.
回答by Hyman Aidley
As pointed out by michaelb958, you cannot replace in place with data of a different length because this will put the rest of the sections out of place. I disagree with the other posters suggesting you read from one file and write to another. Instead, I would read the file into memory, fix the data up, and then write it out to the same file in a separate step.
正如 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)
Unless you've got a massive file to work with which is too big to load into memory in one go, or you are concerned about potential data loss if the process is interrupted during the second step in which you write data to the file.
除非您有一个大文件要处理,而该文件太大而无法一次性加载到内存中,或者您担心如果在将数据写入文件的第二步过程中过程中断,则可能会丢失数据。
回答by LiPi
My variant, one word at a time on the entire file.
我的变体,在整个文件中一次一个字。
I read it into memory.
我读到了记忆中。
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)
回答by Neamerjell
As Hyman Aidley had posted and J.F. Sebastian pointed out, this code will not work:
正如 Hyman 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)`
But this code WILL work (I've tested it):
但是这段代码会起作用(我已经测试过了):
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()
Using this method, filein and fileout can be the same file, because Python 3.3 will overwrite the file upon opening for write.
使用这种方法,filein 和 fileout 可以是同一个文件,因为 Python 3.3 将在打开写入时覆盖文件。
回答by Zelmik
I have done this:
我已经这样做了:
#!/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 ()
回答by Doc5506
I modified Jayram Singh's post slightly in order to replace every instance of a '!' character to a number which I wanted to increment with each instance. Thought it might be helpful to someone who wanted to modify a character that occurred more than once per line and wanted to iterate. Hope that helps someone. PS- I'm very new at coding so apologies if my post is inappropriate in any way, but this worked for me.
我稍微修改了 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()
回答by Vinit Pillai
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!!!')
回答by Deepak G
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)

