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() -& ...
随机推荐
- viewDidUnload,viewDidLoad,viewWillAppear,viewWillDisappear的作用以及区别
viewDidLoad:在视图加载后被调用 viewWillAppear:视图即将可见时调用.默认情况下不执行任何操作 viewDidAppear: 视图已完全过渡到屏幕上时调用 viewWillDi ...
- Jupyter(Ipython) Notebook 入门
upyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言. 一般用来编写漂亮的交互式文档. 文学编程的读者不是机器,而是人. 我们 ...
- RTL Compiler之synthesis steps
1 synthesis steps 1) Search Paths rc:/> set_attribute lib_search_path path / rc:/> set_attribu ...
- Codeforces_The least round way
B. The least round way time limit per test 2 seconds memory limit per test 64 megabytes input standa ...
- SQL几种常用的函数
函数的种类: 算数函数(数值计算的函数) 字符串函数(字符串操作的函数) 日期函数(用来进行日期操作的函数) 转换函数(用来转换数据类型和值的函数) 聚合函数(用来进行数据聚合的函数) 算数函数(+- ...
- pop的运用
pop():弹出列表最后一个元素 练习题: num_list = [12, 45, 34,13, 100, 24, 56, 74, 109] one_list = [] two_list = [] t ...
- Lua之尾调函数的用法
Lua之尾调函数的用法 --当函数的最后返回结果调用另一个函数,称之为尾调函数 function f(x) return g(x) end --由于“尾调用”不会耗费栈空间,所以一个程序可以拥有无数嵌 ...
- STL中队列queue的用法
头文件:#include <queue> 建立一个队列queue < 类型 > q 加入一个新的元素q.push(a) 询问队首元素q.front() 弹出队首元素q.pop( ...
- 50.percentiles百分比算法以及网站延时统计
主要知识点 percentiles的用法 现有一个需求:比如有一个网站,记录下了每次请求的访问的耗时,需要统计tp50,tp90,tp99 tp50:50%的请求的耗时最长在多长时间 tp90 ...
- Java Web项目实战第1篇之环境搭建
写在前面的话 从今天开始一个Java Web实战项目,参考自 http://blog.csdn.net/eson_15/article/details/51277324 这个博客(非常感谢博主的分享精 ...