【问题标题】:long complicated string into json or dictionary将长复杂的字符串转换为 json 或字典
【发布时间】:2013-03-07 21:55:50
【问题描述】:

是否可以将以下string 转换为字典或json 对象:

{"created_at": "Thu Feb 19 11:28:52 +0000 2009", "description": "Le p\u00f4le de comp\u00e9titivit\u00e9 de la fili\u00e8re des contenus 等服务 num\u00e9riques。 http://www.capdigital.com // http://www.facebook.com/capdigitalparisregion", "favourites_count": 7, “followers_count”:5996,“friends_count”:671,“geo_enabled”:真, “id”:21290877,“lang”:“en”,“listed_count”:344,“location”: “巴黎”,“名称”:“Cap Digital”,“profile_background_color”:“920707”, “profile_background_tile”:真,“profile_image_url”: "http://a0.twimg.com/profile_images/2263411541/cap_square_normal.png", “profile_link_color”:“0084B4”,“profile_sidebar_fill_color”: "http://a0.twimg.com/profile_background_images/157539330/xad3a63c73eff9ba8f89f81e01c10a4d.jpg", “profile_text_color”:“333333”,“受保护”:假,“screen_name”: “Cap_Digital”、“status”:{“created_at”:“Mon Mar 18 16:23:05 +0000 2013”​​,“收藏”:假,“id”:313687008188956672,“retweet_count”: 5、“转推”:假,“来源”:“http://clocktweets.com/\” rel=\"nofollow\">Clocktweets", "text": "Plus que quelques jours 先锋@5Plus_Forum,conf\u00e9rence sur la ville smarte ! http://t.co/GV5o6fiqUh", "截断": false}, "statuses_count": 2121, "time_zone": "巴黎", "url": "http://www.capdigital.com", "utc_offset": 3600}

我试过json.load() 它给出了以下回溯:

Traceback (most recent call last):
  File "C:\Users\schatterjee\workspace\twittermining\main.py", line 82, in <module>
    treat_files()      
  File "C:\Users\schatterjee\workspace\twittermining\main.py", line 74, in treat_files
    d = json.load(row[0])
  File "C:\Python27\lib\json\__init__.py", line 274, in load
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

【问题讨论】:

  • 你应该使用 json.loads() 来加载字符串。

标签: python json


【解决方案1】:

您应该使用json.loads()(用s 表示“字符串”)而不是json.load()

【讨论】:

    【解决方案2】:

    您提交的字符串中有两个错误,修改后的字符串被正确解析为 JSON

    {'created_at': 'Thu Feb 19 11:28:52 +0000 2009',
     'description': 'Le p\\u00f4le de comp\\u00e9titivit\\u00e9 de la fili\\u00e8re des contenus et services num\\u00e9riques. http://www.capdigital.com // http://www.facebook.com/capdigitalparisregion',
     'favourites_count': 7,
     'followers_count': 5996,
     'friends_count': 671,
     'geo_enabled': True,
     'id': 21290877,
     'lang': 'en',
     'listed_count': 344,
     'location': 'Paris',
     'name': 'Cap Digital',
     'profile_background_color': '920707',
     'profile_background_tile': True,
     'profile_image_url': 'http://a0.twimg.com/profile_images/2263411541/cap_square_normal.png',
     'profile_link_color': '0084B4',
     'profile_sidebar_fill_color': 'http://a0.twimg.com/profile_background_images/157539330/xad3a63c73eff9ba8f89f81e01c10a4d.jpg',
     'profile_text_color': '333333',
     'protected': False,
     'screen_name': 'Cap_Digital',
     'status': {'created_at': 'Mon Mar 18 16:23:05 +0000 2013',
                'favorited': False,
                'id': 313687008188956672L,
                rel=\\"nofollow\\">Clocktweets", # here
                'retweet_count': 5,
                'retweeted': False,
                'source': 'http://clocktweets.com/\\',
                'text': 'Plus que quelques jours avant @5Plus_Forum, conf\\u00e9rence sur la ville intelligente ! http://t.co/GV5o6fiqUh',
                'truncated': False},
     'statuses_count': 2121,
     'time_zone': 'Paris',
     'url': 'http://www.capdigital.com',
     'utc_offset': 3600}
    

    【讨论】:

    • @codious 检查您发布的字符串,ValueError: Expecting , delimiter: line 1 column 1013 (char 1013)
    • @Schoolboy:如果要将字符串粘贴到源代码中,则需要将其作为原始字符串包含:r"""etc etc"""
    【解决方案3】:

    您可以先将字符串转换为类似文件的缓冲区:

    json.load(StringIO.StringIO(your_string))
    

    【讨论】:

    • 可以使用 json.loads 代替
    猜你喜欢
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 2020-11-25
    相关资源
    最近更新 更多