首页 > 解决方案 > 关于json合并到python的问题

问题描述

好吧,我的问题实际上并不是关于合并,而是关于我如何做到不切片整个字典并最终保持一致性。记住我正在读取 json 中未定义大小的 n 个文件。文件是这些

{"intents": [
        {"tag": "greeting",
         "patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up"],
         "responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"],
         "context_set": ""
        }
   ]
}
{"intents": [
        {"tag": "goodbye",
         "patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day"],
         "responses": ["Sad to see you go :(", "Talk to you later", "Goodbye!"],
         "context_set": ""
        }
   ]
}
{"intents": [
        {"tag": "thanks",
         "patterns": ["Thanks", "Thank you", "That's helpful"],
         "responses": ["Happy to help!", "Any time!", "My pleasure"],
         "context_set": ""
        },
        {"tag": "payments",
         "patterns": ["Do you take credit cards?", "Do you accept Mastercard?", "Are you cash only?" ],
         "responses": ["We accept VISA, Mastercard and AMEX", "We accept most major credit cards"],
         "context_set": ""
        }
   ]
}

到目前为止我所做的以及它是如何工作的,它需要任何 json 文件并将其合并以保存到 json 文件。这是代码。

import json
import os

finaljson2 = {"intents" : []}
for filename in os.listdir('/content/'):
    if filename.endswith('.json'):
        with open(os.path.join('/content/', filename)) as file:
            data = json.load(file)
    middle= data["intents"][0]
    finaljson2["intents"].append(middle)

with open('merged.json', "w") as f:
    f.write(json.dumps(finaljson2, indent=2))

最终结果

{
  "intents": [
    {
      "tag": "goodbye",
      "patterns": [
        "cya",
        "See you later",
        "Goodbye",
        "I am Leaving",
        "Have a Good day"
      ],
      "responses": [
        "Sad to see you go :(",
        "Talk to you later",
        "Goodbye!"
      ],
      "context_set": ""
    },
    {
      "tag": "greeting",
      "patterns": [
        "Hi",
        "How are you",
        "Is anyone there?",
        "Hello",
        "Good day",
        "Whats up"
      ],
      "responses": [
        "Hello!",
        "Good to see you again!",
        "Hi there, how can I help?"
      ],
      "context_set": ""
    },
    {
      "tag": "thanks",
      "patterns": [
        "Thanks",
        "Thank you",
        "That's helpful"
      ],
      "responses": [
        "Happy to help!",
        "Any time!",
        "My pleasure"
      ],
      "context_set": ""
    },
    {
      "tag": "goodbye",
      "patterns": [
        "cya",
        "See you later",
        "Goodbye",
        "I am Leaving",
        "Have a Good day"
      ],
      "responses": [
        "Sad to see you go :(",
        "Talk to you later",
        "Goodbye!"
      ],
      "context_set": ""
    },
    {
      "tag": "goodbye",
      "patterns": [
        "cya",
        "See you later",
        "Goodbye",
        "I am Leaving",
        "Have a Good day"
      ],
      "responses": [
        "Sad to see you go :(",
        "Talk to you later",
        "Goodbye!"
      ],
      "context_set": ""
    },
    {
      "tag": "goodbye",
      "patterns": [
        "cya",
        "See you later",
        "Goodbye",
        "I am Leaving",
        "Have a Good day"
      ],
      "responses": [
        "Sad to see you go :(",
        "Talk to you later",
        "Goodbye!"
      ],
      "context_set": ""
    }
  ]
}

最后他只读了第一部分就写了,我已经试着把它放在一个条件中,但效果不太好。这就是为什么我来这里询问是否有人有任何可能可行的不同想法。

标签: pythonjsonmergechatbot

解决方案


试试这个:

import json
import os

finaljson2 = {"intents" : []}
for filename in os.listdir('/content/'):
    if filename.endswith('.json'):
        with open(os.path.join('/content/', filename)) as file:
            data = json.load(file)

            finaljson2["intents"].extend(data.get("intents",[]))

with open('merged.json', "w") as f:
    f.write(json.dumps(finaljson2, indent=2))

推荐阅读