python str转dict
来源:互联网 发布:江东陆伯言 知乎 编辑:程序博客网 时间:2024/05/19 19:56
两种方法
捷径
eval(str)
>>> user = "{'name' : 'jim', 'sex' : 'male', 'age': 18}">>> type(user)<type 'str'>>>> b=eval(user)>>> >>> b{'age': 18, 'name': 'jim', 'sex': 'male'}>>> type(b)<type 'dict'>
关于eval()的说法,官方demo解释为:将字符串str当成有效的表达式来求值并返回计算结果。
实际上这是有局限的,例如处理多维字典就不行了
json
另一种专业的转换工具是json
>>> user = '{"name":"jim","sex":"male","age":"18"}'>>> json.loads(user){u'age': u'18', u'name': u'jim', u'sex': u'male'}>>> type(user)<type 'str'>>>> type(json.loads(user))<type 'dict'>
阅读全文
0 0
- python str转dict
- python str转dict
- python中list,str,json,dict使用
- Python基础数据结构(list, str, tuple, dict)
- python数据类型互换dict/list/tuple/str互换方法
- [转]Python Dict 用法
- python string 转dict
- python dict转xml
- python unicode转str
- Python List转str
- python str转bytes
- python 中Dict 转 Json
- python两个list转dict
- Python中model转dict
- python json 转 dict , list
- python基础31[list+tuple+set+dict+str+file的成员方法]
- python中的列表(list),字典(dict),字符串(str)的基本操作
- Python中list、tuple、str和dict之间的相互转换
- 概率论12 矩与矩生成函数
- 概率论13 中心极限定律
- 计算机网络概述
- 藏宝图
- 1055. The World's Richest (25)
- python str转dict
- Ruby中文乱码问题
- 数据类型转换之 CString与int
- Ruby on Rails安装过程,煞费苦心
- Rails核心组件
- HDOJ 2039 三角形
- 创建一个Rails项目
- 1093. Count PAT's (25)
- 解决Ubuntu的root账号无法登录SSH问题-Permission denied, please try again.