美文网首页
python 字典/string转换

python 字典/string转换

作者: vieo | 来源:发表于2020-04-21 16:07 被阅读0次

在Python 中的“dict”和“str”类型转换:

第一种:“dict”转为“str”:mystr=str(dict1)

          “str”转为“dict”:①mydict=eval(mystr)

                                   ②:

user="{'name' : 'jim', 'sex' : 'male', 'age': 18}"
exec("c="+user)
c
{'name': 'jim', 'sex': 'male', 'age': 18}
type(user)
<class 'str'>
type(c)
<class 'dict'>

第二种: 使用simplejson把JSON转化为Python内置类型

JSON到字典转化:
ret_dict = simplejson.loads(json_str)
字典到JSON转化:
json_str = simplejson.dumps(dict)
————————————————
版权声明:本文为CSDN博主「安心Smile」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/anxin997483092/java/article/details/79223410

相关文章

网友评论

      本文标题:python 字典/string转换

      本文链接:https://www.haomeiwen.com/subject/aotpihtx.html