Convert a String representation of a Dictionary to a dictionary?

情景是这样的:http返回的一个字符串,而字符串里面是字典,需要提取里面的参数来使用。

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

思路将将其转成字典,就方便使用了。

1 import ast
2 
3 s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"
4 
5 target_dict = ast.literal_eval(res)
6 print target_dict

更多请参考:传送门


原文地址:https://www.cnblogs.com/Wolfanature/p/6049011.html