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'>
'''

 

posted on 2022-08-22 10:50  oktokeep  阅读(225)  评论(0编辑  收藏  举报