python 字符串转字典

import ast
str_info = '{"name": "test", "age": 18}'
dict_info = ast.literal_eval(str_info)

print("string info type is -->: %s" % (type(str_info)))
print("dict info type is -->: %s" % (type(dict_info)))
print(dict_info)

s_info = "{'name': 'nock', 'age': 18}"
d_info = ast.literal_eval(s_info)

print("s info type is -->: %s" % (type(s_info)))
print("d info type is -->: %s" % (type(d_info)))
print(d_info)
posted @ 2024-05-15 21:54  IT乞丐  阅读(29)  评论(0)    收藏  举报