我如何使用我想要的一组单词替换为正则表达式中的另一组单词?
我给了它最好的镜头,这是我最终的结果(当然它没有做我想要的):
count = 0
count2 = 0
set1 = ['word1','word2','word3']
set2 = ['new_word1','new_word2','new_word3']
with open(file, 'r+') as book:
for line in book:
if count<2:
set1var = set1[count]
set2var = set2[count2]
reg1 = re.sub(r''+set1var,r''+set2var,line)
f.write(reg1)
if line != reg1:
count+=1
count2+=1
答案 0 :(得分:2)
你的意思是要完成这件事:
inputdata=open(file).read()
for initial,final in zip(set1,set2):
inputdata=inputdata.replace(initial,final)
print str #or write to file
答案 1 :(得分:1)
这是一个很好的例子:
>>> import re
>>> set1 = ['word1','word2','word3']
>>> set2 = ['new_word1','new_word2','new_word3']
>>> translation = dict(zip(set1, set2))
>>>
>>>
>>> text = '''
... here I am word1 talking about word2.
... ipsum ipsumipsum ipsum ipsum ipsum ipsum ipsum
... ipsum ipsumipsum word1 ipsum ipsum ipsum ipsum
... ipsum ipsumipsum ipsum ipsum word2 ipsum ipsum
... ipsum ipsumipsum ipsum ipsum ipsum ipsum ipsum
... '''
>>>
>>> # compile a pattern matching any of the words
... pattern = re.compile('(%s)' % '|'.join(set1))
>>>
>>> def translate(match):
... return translation[match.group(0)]
...
>>> # replace with: for line in book:
>>> for line in text.splitlines():
... print pattern.sub(translate, line)
...
here I am new_word1 talking about new_word2.
ipsum ipsumipsum ipsum ipsum ipsum ipsum ipsum
ipsum ipsumipsum new_word1 ipsum ipsum ipsum ipsum
ipsum ipsumipsum ipsum ipsum new_word2 ipsum ipsum
ipsum ipsumipsum ipsum ipsum ipsum ipsum ipsum
>>>
虽然我没有使用count
或count2
,但我认为您想要替换所有出现的事件。
由于我在我的示例中使用文本而不是文件,因此您需要:
with open(outputfile, 'w') as output:
with open(file, 'r+') as book:
for line in book:
output.write(pattern.sub(translate, line))