Tôi muốn có một cuốn từ điển từChuyển đổi một chuỗi thành điển python
>>> page_detail_string = urllib2.urlopen("http://graph.facebook.com/Ideas4India").read()
Nó trả về một chuỗi như
>>> 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}'
Bây giờ tôi muốn chuyển đổi nó vào từ điển mà tôi có thể dễ dàng thực hiện bằng cách sử dụng 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')
nhưng tôi nghĩ rằng nó sẽ ném lỗi này vì
"is_published":true
Có cách nào để tôi có thể chuyển nó sang từ điển bằng cách tước khóa và giá trị ở trên ("is_published": true).
Cảm ơn