Python字符串转为字典方法大全
方法一: 通过内置函数eval
str_info = '{"name": "test", "age": 18}'
dict_info = 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 = eval(s_info)
print("string info type is -->: %s" % (type(s_info)))
print("dict info type is -->: %s" % (type(d_info)))
print(d_info)
F:\python\python35\python.exe E:/code/clself/Test/test_example1.py
string info type is -->: <class 'str'>
dict info type is -->: <class 'dict'>
{'name': 'test', 'age': 18}
string info type is -->: <class 'str'>
dict info type is -->: <class 'dict'>
{'name': 'nock', 'age': 18} Process finished with exit code 0
不过使用eval有一个安全性问题
方法二: 通过json模块处理
import json
str_info = '{"name": "test", "age": 18}'
dict_info = json.loads(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 = json.loads(s_info) print("s info type is -->: %s" % (type(s_info)))
print("d info type is -->: %s" % (type(d_info)))
print(d_info)
结果如下:
F:\python\python35\python.exe E:/code/clself/Test/test_example1.py
string info type is -->: <class 'str'>
dict info type is -->: <class 'dict'>
{'name': 'test', 'age': 18}
Traceback (most recent call last):
File "E:/code/clself/Test/test_example1.py", line 10, in <module>
d_info = json.loads(s_info)
File "F:\python\python35\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "F:\python\python35\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "F:\python\python35\lib\json\decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) Process finished with exit code 1
使用json模块进行转换存在一个问题,由于json语法规定 数组或对象之中的字符串必须使用双引号,不能使用单引号
方法三: 通过ast模块处理【推荐使用】
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)
F:\python\python35\python.exe E:/code/clself/Test/test_example1.py
string info type is -->: <class 'str'>
dict info type is -->: <class 'dict'>
{'name': 'test', 'age': 18}
s info type is -->: <class 'str'>
d info type is -->: <class 'dict'>
{'name': 'test', 'age': 18}
使用ast.literal_eval进行转换既不存在使用json 模块进行转换的问题,也不存在使用eval模块进行转换的安全性问题,因此推荐大家使用ast.literal_eval的方法。
Python字符串转为字典方法大全的更多相关文章
- Python 如何将字符串转为字典
在工作中遇到一个小问题,需要将一个 python 的字符串转为字典,比如字符串: user_info = '{"name" : "john", "ge ...
- Python 字符串转换为字典(String to Dict)
一.需求 为了处理从redis中拿到的value,如下 {"appId":"ct","crawlSts":false,"healt ...
- python字符串/列表/字典互相转换
python字符串/列表/字典互相转换 目录 字符串与列表 字符串与字典 列表与字典 字符串与列表 字符串转列表 1.整体转换 str1 = 'hello world' print(str1.spli ...
- python字符串操作实方法大合集
python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下: #1.去空格及特殊符号 s.st ...
- 【VS开发】CString 转为 char *方法大全
[VS开发]CString 转为 char *方法大全 标签(空格分隔): [VS开发] 方法1: CString strTemp; char szTemp[128]; strTemp = _T(&q ...
- python 列表转为字典的两个小方法
1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...
- python字符串内置方法
网上已经有很多,自己操作一遍,加深印象. dir dir会返回一个内置方法与属性列表,用字符串'a,b,cdefg'测试一下 dir('a,b,cdefg') 得到一个列表 ['__add__', ' ...
- Python 字符串分割的方法
在平时工作的时候,发现对于字符串分割的方法用的比较多,下面对分割字符串方法进行总结一下:第一种:split()函数split()函数应该说是分割字符串使用最多的函数用法:str.split('分割符' ...
- 7.python字符串-内置方法分析
上篇对python中的字符串内置方法进行了列举和简单说明,但这些方法太多,逐一背下效率实在太低,下面我来对这些方法按照其功能进行总结: 1.字母大小写相关(中文无效) 1.1 S.upper() -& ...
随机推荐
- Unity学习-碰撞检测(七)
Unity脚本说明 Unity支持语言: C#(主流),JavaScript,Boo 创建脚本: [Assets-Create-C# Script] 学习案例 布置场景: 添加一个Plane,3个Cu ...
- python gdal 数组生成图片
cols = array.shape[1]rows = array.shape[0]originX = rasterOrigin[0]originY = rasterOrigin[1]driver = ...
- php中的抽象方法和抽象类
1.什么是抽象方法? 我们在类里面定义的没有方法提的方法就是抽象方法.所谓的没有方法体指的是,在声明的时候没有大括号以及其中的内容,而是直接在声明时在方法名后加上分号结束,另外在声明抽象方法时方 ...
- Angular——流程控制指令
基本介绍 (1)ng-repeat,类似于for循环,对数组进行遍历 (2)ng-switch on,ng-switch-when,类似于switch,case 基本使用 ng-repeat < ...
- JS——冒泡排序
核心思想: 1.外层for循环控制比较的轮数 2.内层for循环控制每轮比较的次数 3.外层每进行一轮比较,内层就少一次比较,因为外层每进行一轮比较都会产生一个最大值 <script> v ...
- [Windows Server 2012] 安装SQL Server 2012
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装SQL S ...
- (转)Hibernate中的多表操作
http://blog.csdn.net/yerenyuan_pku/article/details/70556208 Hibernate中的多表操作 在实际开发中,我们不可能只是简简单单地去操作单表 ...
- Vector 和 Array 区别
1:array 定义的时候必须定义数组的元素个数;而vector 不需要:且只能包含整型字面值常量,枚举常量或者用常量表达式初始化的整型const对象, 非const变量以及需要到运行阶段才知道其值的 ...
- MySQL之中文乱码问题
创建 my.ini 文件,在该文件中添加以下内容,放在安装好的mysql根路径下: [client] default-character-set=utf8 [mysql] # 设置mysql客户端默认 ...
- CentOS6.8 安装python2.7,pip以及yum
由于CentOS6.8里自带的yum所依赖的python是2.6.66版本,但是安装pip至少要求python是2.7版本,因而原有的2.6并不能卸载,又得安装新的2.7.之前安装的时候强制卸载了2. ...