编程语言
首页 > 编程语言> > Python3 dict和str互转

Python3 dict和str互转

作者:互联网

# Python3 dict和str互转

import ast
str_of_dict = "{'key1': 'key1value111', 'key2': 'key2value222'}"
newdict = ast.literal_eval(str_of_dict)
print(type(str_of_dict))
print(type(newdict))


my_dict = {'key1': 'key1value333', 'key2': 'key2value444'}
print(type(my_dict))
print(str(my_dict))
print(type(str(my_dict)))

''' 打印输出内容如下:
<class 'str'>
<class 'dict'>

<class 'dict'>
{'key1': 'key1value333', 'key2': 'key2value444'}
<class 'str'>
'''

 

标签:type,print,dict,str,key2,互转,my
来源: https://www.cnblogs.com/oktokeep/p/16612097.html