【问题标题】:Converting a String into Dictionary python将字符串转换为字典 python
【发布时间】:2013-03-13 19:57:05
【问题描述】:

我想要一本来自

的字典
 >>> 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)将其转换为字典。

谢谢

【问题讨论】:

    标签: python string dictionary python-2.7


    【解决方案1】:

    你得到的是一个json字符串,你应该使用json.loads转换成dict

    import json
    json.loads(page_detail_string)
    

    【讨论】:

      【解决方案2】:

      使用json 模块

      import json
      json.loads(page_detail_string)
      

      要了解有关json 模块的更多信息,请查看http://docs.python.org/2/library/json.html

      【讨论】:

        【解决方案3】:

        使用json module

        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}
        

        【讨论】:

          猜你喜欢
          • 2018-05-19
          • 2017-03-23
          • 1970-01-01
          • 1970-01-01
          • 2020-11-17
          • 1970-01-01
          • 2013-02-03
          • 2011-06-10
          • 2011-03-16
          相关资源
          最近更新 更多