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() -& ...
随机推荐
- 7CSS之超链接
<!-- title="这是鼠标悬停时显示的文字" 鼠标悬停时,显示相关的文字--> <a href="#" title="这是鼠标 ...
- C++ 由虚基类 虚继承 虚函数 到 虚函数表
//虚基类:一个类可以在一个类族中既被用作虚基类,也被用作非虚基类. class Base1{ public: Base1(){cout<<"Construct Base1!&q ...
- [ USACO 2018 OPEN ] Out of Sorts (Platinum)
\(\\\) \(Description\) 对一长为\(N\)的数列\(A\)排序,不保证数列元素互异: 数列\(A\)中\(A[1...i]\)的最大值不大于\(A[i+1-N]\)的最小值,我们 ...
- LAMP配置课程基础知识详解
听了一天的课程,我本人对这个还是很感兴趣的. [root@localhost~]# root 用户名 localhost 本地 ~ 家目录 不同用户不同 #当前用户是管理员 $当前用户是普通 ...
- html中设置浏览器解码方式
通过添加一行标签: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"& ...
- SQL基本操作——DROP撤销索引、表以及数据库
DROP撤销索引.表以及数据库 --DROP INDEX 命令删除表格中的索引 DROP INDEX table_name.index_name --DROP TABLE 语句删除表(表的结构.属性以 ...
- CSS3:变换和动画
<html> <style> .container{ -webkit-perspective: 800; -webkit-perspective-origin: 50% 40% ...
- PHP 之ftp客户端类封装实现
<?php /** * Class FtpClient */ class FtpClient { private $host = '';//远程服务器地址 private $user = ''; ...
- 在对话框添加bitmap
CBitmap bitmap; //加载指定位图资源 Bmp图片ID bitmap.LoadBitmap(IDB_BITMAP1); //获取对话框上的句柄 图片控件ID CStatic *p = ( ...
- flutter 环境搭建
环境: ladder什么的是必不可少的 win10 + Idea 2019.1.13 + Genymotion 2.12 基本可以在模拟器中运行项目,还有些许小问题,但是可以看到效果了 基本流程 下载 ...