1

正規表現内で別の単語セットに置き換えたい単語セットをどのように使用すればよいですか?

私はそれに最善を尽くしましたが、これが最終的な結果です(もちろん、それは私が望むことをしません):

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
4

2 に答える 2

2

これを成し遂げるつもりですか:

inputdata=open(file).read()
for initial,final in zip(set1,set2):
    inputdata=inputdata.replace(initial,final)
print str #or write to file
于 2013-07-15T18:39:34.433 に答える
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
>>> 

countorは使用していませんcount2が、すべてのオカレンスを置き換えたいと思います。私の例ではファイルではなくテキストを使用しているため、必要なものは次のとおりです。

with open(outputfile, 'w') as output:
    with open(file, 'r+') as book:
        for line in book:
            output.write(pattern.sub(translate, line))
于 2013-07-15T18:55:08.803 に答える