python dict_str.py

'''
此模块用于,探索python字典和字符串之间的相互转换。
'''
# 方法一:
import json
str1 = '{"name": "ljq", "age": 24}'
d1 = json.loads(str1)
print("d1:", d1)
# d1: {'name': 'ljq', 'age': 24}
print(type(d1))
# <class 'dict'>
str1 = json.dumps(d1)
print("str1:", str1)
# str1: {"name": "ljq", "age": 24}
print(type(str1))
# <class 'str'>


# 方法二:
d1 = {"a": 1, "b": 2}
str1 = str(d1)
print("str1:", str1)
# str1: {'a': 1, 'b': 2}
print(type(str1))
# <class 'str'>
d1 = eval(str1)
print("d1:", d1)
# d1: {'a': 1, 'b': 2}
print(type(d1))
# <class 'dict'>
 

猜你喜欢

转载自blog.csdn.net/weixin_42193179/article/details/88074658