Home > Mobile >  Splitting a nested dictionary into several dicts
Splitting a nested dictionary into several dicts

Time:10-23

I want to split the following, nested dictionary into different dictionaries by language AND create a new JSON-File/Dictionary for each language.

Afterwards I would like to merge them back together.

Grateful for any suggestions how to continue!

Example:

{
  "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"
  },

What I got so far:

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)
            .....'''

CodePudding user response:

Going through your dictionary with a loop:

from pprint import pprint

data = {
  "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"
  }
}
new_data = dict()
for word,transl_dict in data.items():
    for lbl, transl in transl_dict.items():
        if not(lbl in new_data.keys()):
            new_data[lbl] = dict()
        new_data[lbl][word] = transl

pprint(new_data)

Output:

{'label_en_EN': {'All': 'All', 'Searchprofile': 'Search profile'},
 'label_es_ES': {'All': 'Todo', 'Searchprofile': 'Perfil de búsqueda'},
 'label_fr_FR': {'All': 'Tout', 'Searchprofile': 'Profil de recherche'},
 'label_it_IT': {'All': 'Tutto', 'Searchprofile': 'Profilo di ricerca'}}

You can of course dump the label_... dictionaries to files individually.

Edit: to output your original expected dictionaries it's even shorter if you already know which labels there are:

labels = ["label_es_ES", "label_it_IT", "label_en_EN", "label_fr_FR"]
for label in labels:
    label_dict = {x: {label: data[x][label]} for x in data}
    pprint(label_dict)
    # or dump directly to files;
    with open(f"{label}.json", "w", encoding="utf-8") as f:
        json.dump(label_dict, f, indent=4, ensure_ascii=False)

Json files are written in utf-8 format so that you see special characters in the json. Don't forget to specify the encoding (utf-8) while opening the file later!

CodePudding user response:

from itertools import islice

def chunks(data, SIZE=10000): it = iter(data) for i in range(0, len(data), SIZE): yield {k:data[k] for k in islice(it, SIZE)}

for item in chunks({i:i for i in range(10)}, 3): print item

  • Related