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

Python3 dict和str互转

时间:2022-08-22 11:02:35浏览次数:68  
标签:type print dict str 互转 my

# 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,互转,my
From: https://www.cnblogs.com/oktokeep/p/16612097.html

相关文章