Riska Nanda
Riska Nanda

Reputation: 129

Converting value to key from dictionary

I have a txt file with the following text:

water=45 
melon=8 
apple=35 
pineapple=67 
I=43 
to=90 
eat=12 
tastes=100 
sweet=21 
it=80 
watermelon=98 
want=70

and I have another file with the following text:

I want to eat watermelon
it tastes sweet pineapple

I want to output into:

I want to eat watermelon = 43,70,90,12,98
it tastes sweet pineapple = 80,100,21,67

This is what I have so far:

import nltk 
f = open(r'C:\folder\dic\file.txt','r')
answer = {}
for line in f:
     k, v = line.strip().split('=')
     answer[k.strip()] = v.strip()

f.close()

print answer.values()

h = open(r'C:\folder\dic\file2.txt','r')
raw=h.read()
tokens = nltk.sent_tokenize(raw)
text = nltk.Text(tokens)


for line in text:
    word = line
    for value in answer.values():
        if value == word:
            word=answer[keys]
        else:
            word="not found"

 print word

What would be the best way to do this in Python?

Upvotes: 1

Views: 118

Answers (1)

Dinesh Pundkar
Dinesh Pundkar

Reputation: 4196

Please check this code.

import re
f = open(r'C:\Users\dinesh_pundkar\Desktop\val.txt','r')
val_dict = {}
for line in f:
     k, v = line.strip().split('=')
     val_dict[k.strip()] = v.strip()
f.close()

print val_dict

h = open(r'C:\Users\dinesh_pundkar\Desktop\str_txt.txt','r')
str_list = []
for line in h:
     str_list.append(str(line).strip())

print str_list

tmp_str = ''
for val in str_list:
    tmp_str = val 
    for k in val_dict.keys():
            if k in val:
                replace_str = str(val_dict[k]).strip() + ","
                tmp_str= re.sub(r'\b{0}\b'.format(k),replace_str,tmp_str,flags=re.IGNORECASE)

    tmp_str = tmp_str.strip(",")
    print val, " = ", tmp_str
    tmp_str = ''

Output :

C:\Users\dinesh_pundkar\Desktop>python demo.py
{'apple': '35', 'I': '43', 'sweet': '21', 'it': '80', 'water': '45', 'to': '90',
 'taste': '100', 'watermelon': '98', 'want': '70', 'pineapple': '67', 'melon': '
8', 'eat': '12'}
['I want to eat watermelon', 'it taste sweet pineapple']
I want to eat watermelon  =  43, 70, 90, 12, 98
it taste sweet pineapple  =  80, 100, 21, 67

Upvotes: 1

Related Questions