Json is becoming more and more popular. After obtaining strings in json format through python, you can convert them to dict format through the eval function:
>>> A = '{"name": "yct", "age": 10 }'
>>> Eval ()
{'Age': 10, 'name': 'yct '}
Strings and numbers are supported. Other formats do not seem to support:
>>> A = '{"name": "yct", "age": 10, "tmp": dddd }'
>>> Eval ()
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'ddddd' is not defined
Convert Dictionary (dict) to string (string)
We can easily convert the dictionary type to the string type.
The dictionary-to-string conversion can be achieved by traversing all the elements in dict:
For key, value in sample_dic.items ():
Print "\" % s \ ": \" % s \ "" % (key, value)
Convert string to dict)
How can I convert a string into a dictionary?
In fact, it is also very simple, as long as the eval () or exec () function can be implemented.
>>> A = "{'a': 'Hi', 'B': 'there '}"
>>> B = eval ()
>>> B
{'A': 'Hi', 'B': 'there '}
>>> Exec ("c =" +)
>>> C
{'A': 'Hi', 'B': 'there '}
>>>