David Qin
David Qin

Reputation: 33

Replacing exact numbers with words from 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  

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.

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.

code:

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.

thanks again.

Upvotes: 2

Views: 178

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 61032

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))

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.

Upvotes: 2

Related Questions