7

から辞書が欲しい

 >>> page_detail_string = urllib2.urlopen("http://graph.facebook.com/Ideas4India").read()

次のような文字列を返します

>>> 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}'

今、私はそれを辞書に変換したいのですが、これは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')

しかし、私はそれが原因でこのエラーをスローすると思います

"is_published":true

上記のキーと値( "is_published":true)を削除して、辞書に変換する方法はありますか?

ありがとう

4

3 に答える 3

18

取得するのはjson文字列です。json.loadsを使用してdictに変換する必要があります

import json
json.loads(page_detail_string)
于 2013-03-24T07:28:39.467 に答える
8

jsonモジュールを使用する

import json
json.loads(page_detail_string)

jsonモジュールの詳細については、http://docs.python.org/2/library/json.htmlをご覧ください。

于 2013-03-24T07:25:28.900 に答える
7

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}

また、ファイルオブジェクトで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}
于 2013-03-24T07:26:35.740 に答える