In this paper, the method of converting the string type to the dictionary type in Python is described as an example, which is a more practical function. The specific methods are as follows:
A dictionary (dict) to a string (string)
We can easily convert a dictionary (dict) type to a string (string) type.
You can implement dictionary to string conversions by traversing all the elements in Dict:
For key, value in Sample_dic.items ():
print "\%s\": \ "%s\" "% (key, value)
Strings (string) to Dictionary (dict)
How do I convert a string (a) to a dictionary (dict)?
It's also very simple, as long as the eval () or EXEC () functions are implemented.
>>> a = "{' A ': ' Hi ', ' B ': ' There '}"
>>> B = eval (a)
>>> b
{' A ': ' Hi ', ' B ': ' There '}
>>> exec ("c=" + a)
>>> c
{' A ': ' Hi ', ' B ': ' There '}
>>>
Interested friends can debug run this instance to deepen the understanding of the program code.