【问题标题】:how to build the following dictionary from a json file?如何从 json 文件构建以下字典?
【发布时间】: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


    【解决方案1】:

    创建一个新的空字典。添加每个意图名称的键并设置空列表的值。 然后使用您创建的示例文本从循环中插入每个文本键值。

    import json
    
    with open('workspace-car-dashboard-en.json') as json_file:
        data = json.load(json_file)
    
    new_dic = {}
    for element in data['intents']:
        new_dic[element['intent']] = []
        for element2 in element['examples']:
            new_dic[element['intent']] += [element2['text']]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      • 2021-10-26
      • 2017-05-09
      相关资源
      最近更新 更多