将字符串转换为字典python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15596121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a String into Dictionary python
提问by vaibhav1312
I want to have a dictionary from
我想要一本字典
>>> page_detail_string = urllib2.urlopen("http://graph.facebook.com/Ideas4India").read()
It returns a string like
它返回一个字符串
>>> page_detail_string
'{"about":"Ideas for development of India","category":"Community","description":"Platform where you can discuss and share your ideas which you think might help in betterment of our country.\nPlease respect other community members and do not talk politics here.","is_published":true,"talking_about_count":0,"username":"Ideas4India","were_here_count":0,"id":"250014455083430","name":"Ideas 4 India","link":"http:\/\/www.facebook.com\/Ideas4India","likes":23}'
Now i want to convert it to dictionary which i can be easily done by using ast.literal_eval
现在我想将它转换为字典,我可以通过使用 ast.literal_eval 轻松完成
>>> import ast
>>> dict_page = ast.literal_eval(page_detail_string)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "/usr/lib/python2.7/ast.py", line 63, in _convert
in zip(node.keys, node.values))
File "/usr/lib/python2.7/ast.py", line 62, in <genexpr>
return dict((_convert(k), _convert(v)) for k, v
File "/usr/lib/python2.7/ast.py", line 79, in _convert
raise ValueError('malformed string')
but I think it throws this error because of
但我认为它会引发此错误,因为
"is_published":true
Is there any way i can convert it to dictionary by stripping above key and value ("is_published":true).
有什么方法可以通过剥离键和值(“is_published”:true)将其转换为字典。
Thanks
谢谢
采纳答案by Yarkee
What you get is a json string, you shoule use json.loads to convert to dict
你得到的是一个 json 字符串,你应该使用 json.loads 转换为 dict
import json
json.loads(page_detail_string)
回答by Raj
Use the jsonmodule
使用json模块
import json
json.loads(page_detail_string)
To read more about the jsonmodule, check out http://docs.python.org/2/library/json.html
要阅读有关该json模块的更多信息,请查看http://docs.python.org/2/library/json.html
回答by root
Use the json module:
使用json 模块:
In [1]: s = '{"about":"Ideas for development of India","category":"Community","description":"Platform where you can discuss and share your ideas which you think might help in betterment of our country.\nPlease respect other community members and do not talk politics here.","is_published":true,"talking_about_count":0,"username":"Ideas4India","were_here_count":0,"id":"250014455083430","name":"Ideas 4 India","link":"http:\/\/www.facebook.com\/Ideas4India","likes":23}'
In [2]: import json
In [3]: json.loads(s)
Out[3]:
{u'about': u'Ideas for development of India',
u'category': u'Community',
u'description': u'Platform where you can discuss and share your ideas which you think might help in betterment of our country.\nPlease respect other community members and do not talk politics here.',
u'id': u'250014455083430',
u'is_published': True,
u'likes': 23,
u'link': u'http://www.facebook.com/Ideas4India',
u'name': u'Ideas 4 India',
u'talking_about_count': 0,
u'username': u'Ideas4India',
u'were_here_count': 0}
Also, note that you can use json.load(instead of json.loads) directly on the file object:
另外,请注意,您可以直接在文件对象上使用json.load(而不是json.loads):
In [4]: import urllib2
In [5]: json.load(urllib2.urlopen("http://graph.facebook.com/Ideas4India"))
Out[5]:
{u'about': u'Ideas for development of India',
u'category': u'Community',
u'description': u'Platform where you can discuss and share your ideas which you think might help in betterment of our country.\nPlease respect other community members and do not talk politics here.',
u'id': u'250014455083430',
u'is_published': True,
u'likes': 23,
u'link': u'http://www.facebook.com/Ideas4India',
u'name': u'Ideas 4 India',
u'talking_about_count': 0,
u'username': u'Ideas4India',
u'were_here_count': 0}

