用DICT python中的单词替换精确数字 [英] Replacing exact numbers with words from DICT python

查看:124
本文介绍了用DICT python中的单词替换精确数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里浏览了各种示例,但是我不知道发生了什么.感谢您的帮助.

I've looked through various examples on here but I can't figure out what is happening. Any help is appreciated.

我有一个文本文件,我想通过字典将数字转换成单词.

I have a text file from which I want to translate the numbers into words through a dictionary.

由于文本文件太长,我只给出一个简短的示例. 文本文件:

since the text file is too long, I'll just give a short sample. text file:

Movie: 12 15 11 13  
Director: 1 9 2 3  

我有一个用制表符分隔的文件,我认为它已变成字典. dictfile:

where I have a file delimited by tabs which I think I have made into a dict. dictfile:

1 Adam  
2 Lee  
3 Tom  
9 Jones  
11 Hostel  
12 WoW  
13 Home  
15 Surf

到目前为止,我所拥有的代码将遍历文本文件,并仅转换它涉及的第一个数字.

the code i have so far will run through the text file and translate just the first number it comes to.

因此对于数字11,它不会将其替换为Hostel,而是会将其替换为AdamAdam.如果我在数字上加上单词边界\ b,则什么也不会替换.

so for the number 11, instead of replacing it with Hostel, it will replace it with AdamAdam. if i add word boundaries \b to the number, nothing gets replaced.

代码:

f = [i.strip().split('\t') for i in open('dict')]  


with open('new.txt', 'w') as outfile, open('printnumbers') as infile:  
        for line in infile:  
            for oldword, newword in f:  
                line = line.replace(oldword, newword)  
    outfile.write(line)  

最终,我希望能够用一个字典替换一行,而用另一字典替换下一行.我会尝试做更多的研究.

eventually i want to be able to replace one line with one dict and the next line with another. that i'll try to do some more research on.

再次感谢.

推荐答案

首先,我们将从dictfile构建字典,然后将其应用于txtfile

First we'll build a dictionary from dictfile, then we'll apply that dictionary to txtfile

with open('dict.txt') as f:
    d = {a: b for line in f for a,b in line.split()}

with open('outfile.txt') as out, open('infile.txt') as infile:
    for line in infile:
        line = line.split()
        line = [d[word] if word in d else word for word in line]
        out.write(' '.join(line))

您的大问题是没有正确使用split.我尚未测试此代码,因此可能需要一些调整,具体取决于文件的格式.

Your big problem was not using split properly. I haven't tested this code, so it may need some tweaking depending on exactly how the files are formatted.

这篇关于用DICT python中的单词替换精确数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆