我希望这里有一位大师来帮助我。我是一名记者,并迅速将一些Python代码拼凑在一起,在南非的一个巨大的故事中抓取推文,并将它们存储在sqlite数据库中供以后分析。 问题是我将原始json对象(推文)存储为字符串,这意味着我在尝试将其转换回json对象时遇到严重问题,因此我可以访问数据的键等。 是否有可能将其恢复为json格式?我已经尝试过json库和demjson但没有成功。
当我检索它时,这是sqlite数据库中单个记录的格式。
(u'{u\'user\': {u\'follow_request_sent\': None, u\'profile_use_background_image\': True, u\'default_profile_image\': False, u\'id\': 98357054, u\'verified\': False, u\'profile_image_url_https\': u\'https://si0.twimg.com/profile_images/3186479781/4b0ee41e43a0df23f19935a0bf7dcaa6_normal.jpeg\', u\'profile_sidebar_fill_color\': u\'DDEEF6\', u\'profile_text_color\': u\'242124\', u\'followers_count\': 349, u\'profile_sidebar_border_color\': u\'C0DEED\', u\'id_str\': u\'98357054\', u\'profile_background_color\': u\'181B1C\', u\'listed_count\': 6, u\'profile_background_image_url_https\': u\'https://si0.twimg.com/profile_background_images/357707925/lonely-for-the-holidays.png\', u\'utc_offset\': 19800, u\'statuses_count\': 10035, u\'description\': u\'Nuthing 2 describe or 2 much 2 xplain. U decide... Confused? Yes me too.. I am Ever-Willing to have new Xperiences\', u\'friends_count\': 173, u\'location\': u\'Knp-Kol-Mum-Del-Bangalore\', u\'profile_link_color\': u\'0084B4\', u\'profile_image_url\': u\'http://a0.twimg.com/profile_images/3186479781/4b0ee41e43a0df23f19935a0bf7dcaa6_normal.jpeg\', u\'following\': None, u\'geo_enabled\': True, u\'profile_background_image_url\': u\'http://a0.twimg.com/profile_background_images/357707925/lonely-for-the-holidays.png\', u\'name\': u\'\\u092f\\u092eYunk\', u\'lang\': u\'en\', u\'profile_background_tile\': True, u\'favourites_count\': 493, u\'screen_name\': u\'Lafange_Darinde\', u\'notifications\': None, u\'url\': u\'http://wittys-nitty-gritty.blogspot.com/2011_10_01_archive.html\', u\'created_at\': u\'Mon Dec 21 11:40:32 +0000 2009\', u\'contributors_enabled\': False, u\'time_zone\': u\'New Delhi\', u\'protected\': False, u\'default_profile\': False, u\'is_translator\': False}, u\'favorited\': False, u\'entities\': {u\'user_mentions\': [{u\'id\': 5402612, u\'indices\': [24, 36], u\'id_str\': u\'5402612\', u\'screen_name\': u\'BBCBreaking\', u\'name\': u\'BBC Breaking News\'}], u\'hashtags\': [{u\'indices\': [60, 75], u\'text\': u\'OscarPistorius\'}], u\'urls\': [{u\'url\': u\'http://t.co/UYNrPQrx\', u\'indices\': [118, 138], u\'expanded_url\': u\'http://bbc.in/YvDT1M\', u\'display_url\': u\'bbc.in/YvDT1M\'}]}, u\'contributors\': None, u\'truncated\': False, u\'text\': u\'Ab ro ke kya faayda :-P"@BBCBreaking: South African athlete #OscarPistorius weeps in court as his bail hearing begins http://t.co/UYNrPQrx\', u\'created_at\': u\'Tue Feb 19 08:05:08 +0000 2013\', u\'retweeted\': False, u\'in_reply_to_status_id_str\': None, u\'coordinates\': None, u\'in_reply_to_user_id_str\': None, u\'source\': u\'<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>\', u\'in_reply_to_status_id\': None, u\'in_reply_to_screen_name\': None, u\'id_str\': u\'303777222123200512\', u\'place\': None, u\'retweet_count\': 0, u\'geo\': None, u\'id\': 303777222123200512L, u\'possibly_sensitive\': False, u\'in_reply_to_user_id\': None}',)
你可以看到我的问题。我实时收集了这些信息,因此无法重新收集数据。
答案 0 :(得分:2)
import ast
tweet = ast.literal_eval(row[0])
我现在在你的文章中提到了吗? ;)
我真的从这个问题得到了答案,这是相关的:
How do you convert a stringed dictionary to a Python dictionary?