我从here中获取了示例代码。
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()
但是我不知道如何用相应的新词替换多个词。在此示例中,如果我想查找诸如(old_text1,old_text2,old_text3,old_text4)
之类的单词,并用其各自的新单词(new_text1,new_text2,new_text3,new_text4)
代替。
谢谢!
答案 0 :(得分:4)
您可以使用zip
遍历检查字词并替换单词,然后替换。
例如:
checkWords = ("old_text1","old_text2","old_text3","old_text4")
repWords = ("new_text1","new_text2","new_text3","new_text4")
for line in f1:
for check, rep in zip(checkWords, repWords):
line = line.replace(check, rep)
f2.write(line)
f1.close()
f2.close()
答案 1 :(得分:1)
易于使用的re模块
import re
s = "old_text1 old_text2"
s1 = re.sub("old_text" , "new_text" , s)
输出
'new_text1 new_text2'
re.sub 用新文本替换旧文本 re.sub doc https://docs.python.org/3.7/library/re.html#re.sub
答案 2 :(得分:1)
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
我们的方法replace_all()带有2个参数。第一个文本是替换将要发生的字符串或文件(文本)。第二个字典dic是一本字典,其中要替换的单词或字符作为键,而替换单词或字符作为该键的值。如果您只想替换一个单词或字符,那么此词典可以只有一个key:value对,如果您想一次替换多个单词或字符,则该字典可以有多个key:values。
答案 3 :(得分:1)
我了解到,该脚本的工作原理非常好,并且比我过去使用的脚本快得多。
import re
def word_replace(text, replace_dict):
rc = re.compile(r"[A-Za-z_]\w*")
def translate(match):
word = match.group(0).lower()
print(word)
return replace_dict.get(word, word)
return rc.sub(translate, text)
old_text = open('YOUR_FILE').read()
replace_dict = {
"old_word1" : 'new_word1',
"old_word2" : 'new_word2',
"old_word3" : 'new_word3',
"old_word4" : 'new_word4',
"old_word5" : 'new_word5'
} # {"words_to_find" : 'word_to_replace'}
output = word_replace(old_text, replace_dict)
f = open("YOUR_FILE", 'w') #what file you want to write to
f.write(output) #write to the file
print(output) #check that it worked in the console
答案 4 :(得分:1)
您可以使用正则表达式模块( re )中的 sub 替换文本或文件的内容:
def replace_content(dict_replace, target):
"""Based on dict, replaces key with the value on the target."""
for check, replacer in list(dict_replace.items()):
target = sub(check, replacer, target)
return target
或仅使用 str.replace 即可,而无需从重新导入子:
def replace_content(dict_replace, target):
"""Based on dict, replaces key with the value on the target."""
for check, replacer in list(dict_replace.items()):
target = target.replace(check, replacer)
return target
这是完整的实现:
from re import sub
from os.path import abspath, realpath, join, dirname
file = abspath(join(dirname(__file__), 'foo.txt'))
file_open = open(file, 'r')
file_read = file_open.read()
file_open.close()
new_file = abspath(join(dirname(__file__), 'bar.txt'))
new_file_open = open(new_file, 'w')
def replace_content(dict_replace, target):
"""Based on dict, replaces key with the value on the target."""
for check, replacer in list(dict_replace.items()):
target = sub(check, replacer, target)
# target = target.replace(check, replacer)
return target
# check : replacer
dict_replace = {
'ipsum': 'XXXXXXX',
'amet,': '***********',
'dolor': '$$$$$'
}
new_content = replace_content(dict_replace, file_read)
new_file_open.write(new_content)
new_file_open.close()
# Test
print(file_read)
# Lorem ipsum dolor sit amet, lorem ipsum dolor sit amet
print(new_content)
# Lorem XXXXXXX $$$$$ sit *********** lorem XXXXXXX $$$$$ sit amet