How to escape dict from str in python?
Come straight to the point.
A str object below:
s = '{"key1":"value1", "key2":"value2", "key3":"value3"}'
As you see, dict is wrapped in str. Now how to e开发者_C百科scape dict from str?
In other words, is it possible d = operate(s), d["key1"] = "value1"
, and etc?
>>> ast.literal_eval('{"key1":"value1", "key2":"value2", "key3":"value3"}')
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
i'd use json:
try:
import json
except ImportError:
import simplejson as json
d = json.loads(s)
You're looking for eval.
精彩评论