比直接eval更好的方法
>>>import ast
>>>ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}")
{'muffin':'lolz','foo':'kitty'}

用 json 遇到问题:

>>> import json

json.loads('{"x": 1,"y":2}')

{u'y': 2, u'x': 1}
>>> json.loads("{'x': 1,'y':2}")

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 1 (char 1)

原因是  json格式的key  只能用 双引号 "" 包含。

 
05-12 00:27