0
点赞
收藏
分享

微信扫一扫

Python3 dict和str互转

女侠展昭 2024-10-30 阅读 39

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

 

举报

相关推荐

0 条评论