Python转换字符串字典为Python字典类型数据

假设我们有一下字符串:

1
stra = "{'muffin' : 'lolz', 'foo' : 'kitty'}"

可以看到这个字符串非常像python的字典类型,我们期望把它转换为字典类型,然后方便我们读取数据。

大家知道可以通过 eval 函数转换, 但是有一种更加安全的方法, 那就是literal_eval

如下:

1
2
3
import ast
stra = "{'muffin' : 'lolz', 'foo' : 'kitty'}"
ast.literal_eval(stra)

literal_eval的介绍如下:

1
2
3
4
5
6
7
8
>>> help(ast.literal_eval)
Help on function literal_eval in module ast:

literal_eval(node_or_string)
Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the following
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
and None.

给出使用eval和 literal_eval转换时候的出错比较:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>> eval("shutil.rmtree('mongo')")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
File "/opt/Python-2.6.1/lib/python2.6/shutil.py", line 208, in rmtree
onerror(os.listdir, path, sys.exc_info())
File "/opt/Python-2.6.1/lib/python2.6/shutil.py", line 206, in rmtree
names = os.listdir(path)
OSError: [Errno 2] No such file or directory: 'mongo'
>>> ast.literal_eval("shutil.rmtree('mongo')")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/Python-2.6.1/lib/python2.6/ast.py", line 68, in literal_eval
return _convert(node_or_string)
File "/opt/Python-2.6.1/lib/python2.6/ast.py", line 67, in _convert
raise ValueError('malformed string')
ValueError: malformed string

可以看出 literal_eval 比 eval安全得多

0%