python - 将python字符串转换成json格式

标签 python json twitter

希望有高人来帮助我。我是一名记者,我很快就拼凑了一些 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}',)

你可以看到我的问题。我是实时收集的,因此无法重新收集数据。

最佳答案

import ast
tweet = ast.literal_eval(row[0])

现在你的文章中提到我了吗? ;)

我确实从这个问题中得到了答案,这是相关的:

How do you convert a stringed dictionary to a Python dictionary?

关于python - 将python字符串转换成json格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14957218/

相关文章:

java - 将包含 JSON 的字符串转换为列表,然后将该列表添加回 JSON

json - 无法识别 Express REST API 响应方法

api - 推特 API 限制

android - 如何使用 fabric 获取用户时间线?

python - re.findall() 查找所有包含否定项的二元组

python - Types.py 错误、重命名问题、AttributeError

python - 如何通过多行和每行单元格的表格中的部分文本查找元素?

java - Android Volley Json 请求 : Parameters not showing

python - Python 中多个重复的 lambda 函数的内存成本是多少?

c# - 识别twitter用户的经度和纬度