1.eval
2.json
# NameError: name
# 'null' is not defined
# i_dict=eval(i)
这种方式,如果dict字符串中有null ,将不能变成dict
i_dict = json.loads(i)
#将str类型转化为dict policy = json.loads(p) #将dict类型转化为str p = json.dumps(policy)
本文探讨了在Python中使用eval和json模块进行字典类型转换的方法,特别关注如何避免NameError异常,以及处理包含null值的字典字符串。通过对比eval和json.loads,解释了为何在实际应用中推荐使用json.loads进行str到dict的转换。
1.eval
2.json
# NameError: name
# 'null' is not defined
# i_dict=eval(i)
这种方式,如果dict字符串中有null ,将不能变成dict
i_dict = json.loads(i)
#将str类型转化为dict policy = json.loads(p) #将dict类型转化为str p = json.dumps(policy)
转载于:https://www.cnblogs.com/BlueFire-py/p/10059998.html

被折叠的 条评论
为什么被折叠?