python - 关于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": ""
}
]
}
最后他只读了第一部分就写了,我已经试着把它放在一个条件中,但效果不太好。这就是为什么我来这里询问是否有人有任何可能可行的不同想法。
解决方案
试试这个:
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))
推荐阅读
- java - 如何在android中导入特定版本的fabric
- mysql - 在 mysql 上从拉丁语转换为 UTF-8 - 具有特殊字符的密码会发生什么?
- android - RecyclerView插入动画不显示
- ckeditor5 - Ckeditor 5 如何以编程方式触发复制、剪切、粘贴事件
- php - 带有钠整数错误的PHP random_bytes
- java - 从 MainTableEntity 引用 MasterDataEntity 的外键具有错误的列数
- excel - 如果是假期,则查找上一个工作日
- tizen - Tizen 模拟器 - 没有可安装的平台
- c# - 带有字符串 id 的 ASP WebApi GET 响应?
- git - Git - 获取进行更改的文件的方法/函数名称