import json
import csv
from watson_developer_cloud import NaturalLanguageUnderstandingV1
import watson_developer_cloud.natural_language_understanding.features.v1 as \
features
natural_language_understanding = NaturalLanguageUnderstandingV1(
version='2017-02-27',
username='b6dd1781-02e4-4dca-a706-05597d574221',
password='c3ked6Ttmmc1')
response = natural_language_understanding.analyze(
text='Bruce Banner is the Hulk and Bruce Wayne is BATMAN! '
'Superman fears not Banner, but Wayne.',
features=[features.Entities()])
response1 = natural_language_understanding.analyze(
text='Bruce Banner is the Hulk and Bruce Wayne is BATMAN! '
'Superman fears not Banner, but Wayne.',
features=[features.Keywords()])
#print response.items()[0][1][1]
make= json.dumps(response, indent=2)
make1= json.dumps(response1, indent=2)
print make
print make1
x = json.loads(make)
f = csv.writer(open("Entities.csv", "wb+"))
f.writerow(["relevance", "text", "type", "count"])
for x1 in x:
f.writerow([x1['relevance'],
x1['text'],
x1['type'],
x1['count']])
上面的make變量包含一個必須轉換爲CSV的JSON,並且這樣做時我得到一個類型爲TypeError的錯誤:字符串索引必須是整數。實際的問題是我無法通過實體並獲得關鍵值對,有人可以告訴我在這裏可以做些什麼? JSON將JSON轉換爲CSV
{
"entities": [
{
"relevance": 0.931351,
"text": "Bruce Banner",
"type": "Person",
"count": 3
},
{
"relevance": 0.288696,
"text": "Wayne",
"type": "Person",
"count": 1
}
],
"language": "en"
}
請包括產生短節目你所描述的錯誤。請包括您的實際和預期的程序輸出。 –
你可以把數據放在excel中,並記錄將該數據解析成.csv的宏然後你可以將該腳本轉換成python等等...... – DeerSpotter