-1
我正在使用Python 2.7并尝试将一个浮点值插入到一个键中。但是,所有值都被插入为0.0。极性值被插入为0.0而不是实际值。Python 2.7字典值没有采用float作为输入
代码段:
from textblob import TextBlob
import json
with open('new-webmd-answer.json') as data_file:
data = json.load(data_file, strict=False)
data_new = {}
lst = []
for d in data:
string = d["answerContent"]
blob = TextBlob(string)
#print blob
#print blob.sentiment
#print d["questionId"]
data_new['questionId'] = d["questionId"]
data_new['answerMemberId'] = d["answerMemberId"]
string1 = str(blob.sentiment.polarity)
print string1
data_new['polarity'] = string1
#print blob.sentiment.polarity
lst.append((data_new))
json_data = json.dumps(lst)
#print json_data
with open('polarity.json', 'w') as outfile:
json.dump(json_data, outfile)
每个迭代一个新的字典,当你打印你看到预期的输出字符串1?此外,它看起来像覆盖字典中的密钥,每次迭代'd in data' – user2682863
@ user2682863是的,当我打印字符串1时,我看到了预期的输出。是的,我覆盖了钥匙。在我覆盖之前,我还将它添加到列表中。 –
我的答案是否解决了您的问题? – user2682863