0

是否可以将以下内容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 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": 313687008188956672, "retweet_count": 5, "retweeted": false, "source": "http://clocktweets.com /\" rel=\"nofollow\">Clocktweets", "text": "Plus que quelques jours avant @5Plus_Forum, conf\u00e9rence sur la ville intelligencee! 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'
4

3 回答 3

3

您应该使用json.loads()(with an sfor "string") 而不是json.load().

于 2013-03-19T09:42:54.887 回答
1

您提供的字符串中有两个错误,修改后的字符串被正确解析为 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}
于 2013-03-19T09:44:54.267 回答
0

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

json.load(StringIO.StringIO(your_string))
于 2013-03-19T09:43:05.153 回答