Как можно доставать ключи из одного словаря и преобразовывать их в json
Есть словарь...
CLASSES = {
'1':'class-1',
'2':'class-2',
'3':'class-3',
'4':'class-4',
'5':'class-5',
'6':'class-6',
'7':'class-7',
'8':'class-8',
'9':'class-9',
'10':'class-10',
'11':'class-11',
}
Как доставать ключи по очереди из этого словаря и преобразовывать их в json по типу...
keyboard_classes = {
"one_time": False,
"buttons": [
[{
"action": {
"type": "text",
"label": "1 ключ из словаря"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "2 ключ из словаря"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "3 ключ из словаря"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "4 ключ из словаря"
},
"color": "positive"
},
],
[{
"action": {
"type": "text",
"label": "5 ключ из словаря"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "6 ключ из словаря"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "7 ключ из словаря"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "8 ключ из словаря"
},
"color": "positive"
},
],
# и так далее...
]
}
Ответы (3 шт):
Автор решения: Alteration
→ Ссылка
import json
CLASSES = {
'1':'class-1',
'2':'class-2',
'3':'class-3',
'4':'class-4',
'5':'class-5',
'6':'class-6',
'7':'class-7',
'8':'class-8',
'9':'class-9',
'10':'class-10',
'11':'class-11',
}
result = json.dumps(CLASSES, indent=4)
print(result)
Есть модуль json, который по-моему решает вашу проблему, код не тестировал, пробуйте.
Автор решения: MaxU
→ Ссылка
Для гибкости можно воспользоваться модулем dpath:
from copy import deepcopy
import dpath.util as DU # pip install dpath
template = {
"action": {
"type": "text",
"label": "???"
},
"color": "positive"
}
def gen_entry(val, template=template, path="/action/label"):
d = deepcopy(template)
DU.set(d, path, val)
return d
keys = list(CLASSES.keys())
bucket_size = 4
buttons = [[gen_entry(key, template=template, path="/action/label")
for key in keys[i:i+bucket_size]]
for i in range(0, len(keys), bucket_size)]
keyboard_classes = dict(
one_time=False,
buttons=buttons
)
результат:
In [51]: print(json.dumps(keyboard_classes, indent=2))
{
"one_time": false,
"buttons": [
[
{
"action": {
"type": "text",
"label": "1"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "2"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "3"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "4"
},
"color": "positive"
}
],
[
{
"action": {
"type": "text",
"label": "5"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "6"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "7"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "8"
},
"color": "positive"
}
],
[
{
"action": {
"type": "text",
"label": "9"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "10"
},
"color": "positive"
},
{
"action": {
"type": "text",
"label": "11"
},
"color": "positive"
}
]
]
}
Автор решения: Михаил Муругов
→ Ссылка
Без сторонних библиотек.
from itertools import islice
keyboard_classes = {
"one_time": False,
"buttons": [
[
{
"action": {
"type": "text",
"label": key
},
"color": "positive"
}
for key in part
]
for part in (islice(CLASSES, i, i + 4) for i in range(0, len(CLASSES), 4))
]
}