开发者

How to escape dict from str in python?

开发者 https://www.devze.com 2023-02-28 18:38 出处:网络
Come straight to the point. A str object below: s = \'{\"key1\":\"value1\", \"key2\":\"value2\", \"key3\":\"value3\"}\'

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消