【发布时间】:2018-05-18 10:28:32
【问题描述】:
您好,我正在使用如下所示的 json 文件:
{
"workspace_id": "car-dashboard-en",
"name": "Car Dashboard - Sample",
"created": "2017-03-20T21:24:41.498Z",
"intents": [
{
"intent": "about_VA",
"created": "2017-03-14T02:02:16.290Z",
"updated": "2017-03-14T02:02:16.290Z",
"examples": [
{
"text": "any music reccomendation?",
"created": "2017-03-14T02:02:16.290Z",
"updated": "2017-03-14T02:02:16.290Z"
},
{
"text": "can you recommend ood jazz music?",
"created": "2017-03-14T02:02:16.290Z",
"updated": "2017-03-14T02:02:16.290Z"
},
{
"text": "cloud you recommend music?",
"created": "2017-03-14T02:02:16.290Z",
"updated": "2017-03-14T02:02:16.290Z"
},
{
"text": "your name",
"created": "2017-03-14T02:02:16.290Z",
"updated": "2017-03-14T02:02:16.290Z"
}
],
"description": null
},
{
"intent": "capabilities",
"created": "2017-03-14T02:02:16.290Z",
"updated": "2017-03-14T02:02:16.290Z",
"examples": [
{
"text": "adapt to current weather condition",
"created": "2017-03-14T02:02:16.290Z",
"updated": "2017-03-14T02:02:16.290Z"
},
{
"text": "book a flight for NY on sunday",
"created": "2017-03-14T02:02:16.290Z",
"updated": "2017-03-14T02:02:16.290Z"
},
{
"text": "can you change lanes",
"created": "2017-03-14T02:02:16.290Z",
"updated": "2017-03-14T02:02:16.290Z"
},
我有一个 json,它有一个称为“意图”的部分,这部分由多个意图组成,每个意图都有一个名为“文本”的值列表,
我想从这个文件中提取一个字典如下:
dictionary = {'intent_name':[text1,text1,text3,...],'intent_name2':[text1,text2,text3,...],...}
这个字典的键是关联的意图的名称,值是这个意图的每个文本,
不幸的是,我是一个使用 json 文件的初学者,所以我尝试了:
import json
with open('workspace-car-dashboard-en.json') as json_file:
data = json.load(json_file)
使用以下代码,我提取并打印所有文本值,如下所示:
for element in data['intents']:
for element2 in element['examples']:
print(element2['text'])
我明白了:
any music reccomendation?
can you recommend ood jazz music?
cloud you recommend music?
do you hate
Do you like
do you love
但是,我无法即时构建字典并对所有文本值进行附加,因此我非常感谢支持以完成这项艰巨的任务。 我无法包含完整的 json 文件,因为它非常大,所以我希望这个例子足够了。
【问题讨论】:
标签: arrays python-3.x pandas dictionary