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