Skip to main content
 首页 » 编程设计

python之用另一个文件中的单词替换替换单词

2025年05月04日17wayfarer

我的文本文件 (mytext.txt) 中的单词需要替换为另一个文本文件 (replace.txt) 中提供的其他单词

cat mytext.txt 
this is here. and it should be there.  
me is this will become you is that. 
 
cat replace.txt 
this that 
here there 
me you 

以下代码未按预期工作。

with open('mytext.txt', 'r') as myf: 
    with open('replace.txt' , 'r') as myr: 
        for line in myf.readlines(): 
            for l2 in myr.readlines(): 
                original, replace = l2.split() 
                print line.replace(original, replace) 

预期输出:

that is there. and it should be there.  
you is that will become you is that. 

请您参考如下方法:

编辑: 我的观点是正确的,OP 要求逐字替换而不是简单的字符串替换('become' -> 'become' 而不是 'becoyou')。我想一个字典版本可能看起来像这样,使用在 Splitting a string into words and punctuation 的已接受答案的评论中找到的正则表达式拆分方法。 :

import re 
 
def clean_split(string_input): 
    """  
    Split a string into its component tokens and return as list 
    Treat spaces and punctuations, including in-word apostrophes as separate tokens 
 
    >>> clean_split("it's a good day today!") 
    ["it", "'", "s", " ", "a", " ", "good", " ", "day", " ", "today", "!"] 
    """ 
    return re.findall(r"[\w]+|[^\w]", string_input) 
 
with open('replace.txt' , 'r') as myr: 
    replacements = dict(tuple(line.split()) for line in myr) 
 
with open('mytext.txt', 'r') as myf: 
    for line in myf: 
        print ''.join(replacements.get(word, word) for word in clean_split(line)), 

我无法很好地推理re 效率,如果有人指出明显的低效率,我将不胜感激。

编辑 2: 好吧,我在单词和标点符号之间插入空格,现在 通过将空格视为标记并执行 ''.join() 来修复 而不是 ' '.join()