【发布时间】:2021-12-09 05:34:58
【问题描述】:
我想按语言将以下嵌套字典拆分为不同的字典并为每种语言创建一个新的 JSON 文件/字典。
之后我想将它们重新合并在一起。
感谢任何关于如何继续的建议!
示例:
{
"All": {
"label_es_ES": "Todo",
"label_it_IT": "Tutto",
"label_en_EN": "All",
"label_fr_FR": "Tout"
},
"Searchprofile": {
"label_es_ES": "Perfil de búsqueda",
"label_it_IT": "Profilo di ricerca",
"label_en_EN": "Search profile",
"label_fr_FR": "Profil de recherche"
},
到目前为止我得到了什么:
import json
store_file = open( 'test.txt' , "w" )
with open('translations.json') as json_file:
data = json.load(json_file)
for label, translations in data.items():
for key in translations:
if key==('label_en_EN'):
json.dump(???, store_file)
.....'''
【问题讨论】:
-
您能否详细说明您希望如何格式化各个 JSON 文件?根据您提供的输入判断,为每种语言创建一个新的 JSON 文件只是一个键值对。 IE。
"label_es_ES": "Perfil de búsqueda" -
感谢您的快速回复!!单个字典的格式应保持不变,但只包含一种语言:{ "All": { "label_es_ES": "Todo", }, "Searchprofile": { "label_es_ES": "Perfil de búsqueda", },
标签: python json dictionary split nested