Python 中将字符串转换为字典的方法

您所在的位置:网站首页 字典方法 Python 中将字符串转换为字典的方法

Python 中将字符串转换为字典的方法

2023-06-30 07:41| 来源: 网络整理| 查看: 265

Python 如何将字符串转为字典

Python 如何将字符串转为字典

在工作中遇到一个小问题,需要将一个 python 的字符串转为字典,比如字符串:

user_info = '{"name" : "john", "gender" : "male", "age": 28}'

我们想把它转为下面的字典:

user_dict = {"name" : "john", "gender" : "male", "age": 28}

有以下几种方法:

1、通过 json 来转换

>>> import json >>> user_info= '{"name" : "john", "gender" : "male", "age": 28}' >>> user_dict = json.loads(user_info) >>> user_dict {u'gender': u'male', u'age': 28, u'name': u'john'}

但是使用 json 进行转换存在一个潜在的问题。

由于 json 语法规定 数组或对象之中的字符串必须使用双引号,不能使用单引号 (官网上有一段描述是 “A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes” ),因此下面的转换是错误的:

>>> import json >>> user_info = "{'name' : 'john', 'gender' : 'male', 'age': 28}" # 由于字符串使用单引号,会导致运行出错 >>> user_dict = json.loads(user_info) Traceback (most recent call last): File "", line 1, in File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 380, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 1 column 2 (char 1)

2、通过 eval

>>> user_info = '{"name" : "john", "gender" : "male", "age": 28}' >>> user_dict = eval(user_info) >>> user_dict {'gender': 'male', 'age': 28, 'name': 'john'} >>> user_info = "{'name' : 'john', 'gender' : 'male', 'age': 28}" >>> user_dict = eval(user_info) >>> user_dict {'gender': 'male', 'age': 28, 'name': 'john'}

通过 eval 进行转换就不存在上面使用 json 进行转换的问题。但是,使用 eval 却存在安全性的问题,比如下面的例子:

user_info

#

再输入一些删除命令,则可以把整个目录清空了! >>> user_dict = eval(user_info)

3、通过 literal_eval

>>> import ast >>> user = '{"name" : "john", "gender" : "male", "age": 28}' >>> user_dict = ast.literal_eval(user) >>> user_dict {'gender': 'male', 'age': 28, 'name': 'john'} user_info = "{'name' : 'john', 'gender' : 'male', 'age': 28}" >>> user_dict = ast.literal_eval(user) >>> user_dict {'gender': 'male', 'age': 28, 'name': 'john'}

使用 ast.literal_eval 进行转换既不存在使用 json 进行转换的问题,也不存在使用 eval 进行转换的 安全性问题,因此推荐使用 ast.literal_eval。

参考资料

JSONpython: single vs double quotes in JSON - Stack OverflowUsing python’s eval() vs. ast.literal_eval()? - Stack OverflowJSON对象 – JavaScript 标准参考教程(alpha)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3