字典 => 字符串

1
2
3
dict1 = {'one':1, 'two':2, 'three': {'three.1': 3.1, 'three.2': 3.2 }}
str1 = str(dict1)
print(str1)

字符串 => 字典

1
2
3
4
5
dict1 = {'one':1, 'two':2, 'three': {'three.1': 3.1, 'three.2': 3.2 }}
str1 = str(dict1)

dict2 = eval(str1)
print(dict2)

参考

  1. http://stackoverflow.com/questions/4547274/convert-a-python-dict-to-a-string-and-back

留言