我有一个带有以下文字的txt文件:
water=45
melon=8
apple=35
pineapple=67
I=43
to=90
eat=12
tastes=100
sweet=21
it=80
watermelon=98
want=70
我有另一个文件,其中包含以下文字:
I want to eat watermelon
it tastes sweet pineapple
我想输出到:
I want to eat watermelon = 43,70,90,12,98
it tastes sweet pineapple = 80,100,21,67
这是我到目前为止所做的:
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
在Python中执行此操作的最佳方法是什么?
答案 0 :(得分:1)
请检查此代码。
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
输出:
height: 100%;