Answer the question
In order to leave comments, you need to log in
How to get a dictionary from a string (from str to dict)?
Good afternoon guys.
I have a line:
sunofdog = "{'d': '19', 'mon': '09', 'month': 'Сентябрь', 'address': 'Г. Кемерово ул. Ноградская д. 16, кв. 1'}"
dicktator = превращение_в_словарь(sunofdog) # Это например
print dicktator['month']
---
Сентябрь
dicktator = dict(e.split(': ') for e in sunofdog.split(', '))
import json
json.dumps(sunofdog)
Answer the question
In order to leave comments, you need to log in
Can be cast to standard json or eval(regular or safe from ast package)
>>> sunofdog = "{'d': '19', 'mon': '09', 'month': 'Сентябрь', 'address': 'Г. Кемерово ул. Ноградская д. 16, кв. 1'}"
>>> a = json.loads(sunofdog.replace("'",'"'))
>>> type(a)
"<class 'dict'>"
>>> b = eval(sunofdog)
>>> type(b)
"<class 'dict'>"
>>> a
{'d': '19', 'mon': '09', 'month': 'Сентябрь', 'address': 'Г. Кемерово ул. Ноградская д. 16, кв. 1'}
>>> a == b
True
>>>
>>> import ast
>>> c = ast.literal_eval(sunofdog)
>>> a == c
True
>>>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question