/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
json_examples/load_with_replacing_value.py
43 строки
1 KB
gil9red
Refactoring. Using black code style
28 май 2023, 14:42
28 май 2023, 14:42
5764e48
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" import json def dict_clean(items, default): return {k: default if v is None else v for k, v in items} text = """ { "Action-bar": null, "Action": "Action", "Children": [ {"Action": null}, {"Action": true}, {"Action": "false"}, {"Action": {"need": null}} ], "RGB-bar": null } """ genre_translate = json.loads( text, encoding="utf-8", object_pairs_hook=lambda items: dict_clean(items, default=[]), ) print(genre_translate) # {'Action-bar': [], 'Action': 'Action', 'Children': [{'Action': []}, {'Action': True}, {'Action': 'false'}, {'Action': {'need': []}}], 'RGB-bar': []} genre_translate = json.loads( text, encoding="utf-8", object_pairs_hook=lambda items: dict_clean(items, default="<null>"), ) print(genre_translate) # {'Action-bar': '<null>', 'Action': 'Action', 'Children': [{'Action': '<null>'}, {'Action': True}, {'Action': 'false'}, {'Action': {'need': '<null>'}}], 'RGB-bar': '<null>'}