使用di替换文件中的文本

2024-10-06 06:53:09 发布

您现在位置:Python中文网/ 问答频道 /正文

有5404英寸”无语言.txt". 示例

...
2mz   tomorrow
2night   tonight
2nite   tonight
soml   story of my life
ssry   so sorry
...

在“测试.txt““

^{2}$

我的代码:

 NoSlang = open("noslang.txt")
 for line in NoSlang:
      slang,fulltext = map(str, line.split('\t'))
      dic[slang] = fulltext.strip('\n')


 file = open('test.txt').read().split("\n")
 for line in file:
     sline = line.split(" ")
     for n,i in enumerate(sline):
         if i in dic:
             sline[n] = dic[i]
     print ' '.join(sline)

我试着创造一本字典,并把它们替换成测试.txt". 结果也一样,没有什么变化。在

有什么建议吗?在

预期结果:

 yeah  right
 i'll attend the class
 tomorrow will be great

Tags: intxtforlineopentomorrowfilesplit
2条回答

可以使用正则表达式替换文件中的单词:

#!/usr/bin/env python
import re
from functools import partial

with open('noslang.txt') as file:
    # slang word -> translation
    slang_map = dict(map(str.strip, line.partition('\t')[::2])
                     for line in file if line.strip())

slang_words = sorted(slang_map, key=len, reverse=True) # longest first for regex
regex = re.compile(r"\b({})\b".format("|".join(map(re.escape, slang_words))))
substitute_slang = partial(regex.sub, lambda m: slang_map[m.group(1)])

with open('input.txt') as file:
    for line in file:
        print substitute_slang(line),

如果input.txt不是很大,可以一次替换所有俚语:

^{pr2}$

像这样:

with open('noslang.txt') as f:
    dic = dict(line.strip().split(None,1) for line in f)
...     
with open('test.txt') as f:
    for line in f:                                             
        spl = line.split()
        new_lis =[dic.get(word,word) for word in spl]
        print " ".join(new_lis)
...         
yeah right
i'll attend the class
tomorrow will b great

其中noslang.txt包含:

^{pr2}$

相关问题 更多 >