-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)
の反復ごとに新しい辞書を作りますか?また、それはあなたが 'データのd'の各反復で辞書のキーを上書きするように見える – user2682863
@ user2682863はい、私は文字列1を印刷すると、期待される出力が表示されます。はい、私はキーを上書きしています。上書きする前に、リストにも追加します。 –
私の答えはあなたの問題を解決しましたか? – user2682863