码迷,mamicode.com
首页 > 编程语言 > 详细

Python字符串转为字典方法大全

时间:2018-05-03 20:00:03      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:round   .exe   因此   module   RoCE   nis   col   ble   双引号   

方法一: 通过内置函数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字符串转为字典方法大全

标签:round   .exe   因此   module   RoCE   nis   col   ble   双引号   

原文地址:https://www.cnblogs.com/dieyaxianju/p/8986430.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!