python - 在 Python 中解析 Twitter JSON JSONDecodeError : Expecting value: line 2 column 1 (char 2)

标签 python json twitter

我正在尝试解析另一位研究人员捕获的推文文本文件。前两行是:

    {"firstpost_date": 1435805238, "title": "#Jetlounge. #100Days100Nights #NOLA. #CWMB3 https://t.co/0B8c0h1PwS", "url": "http://twitter.com/DANIELCP3/status/616437964219023360", "tweet": {"contributors": null, "truncated": false, "text": "#Jetlounge. #100Days100Nights #NOLA. #CWMB3 https://t.co/0B8c0h1PwS", "in_reply_to_status_id": null, "id": 616437964219023360, "favorite_count": 0, "source": "<a href=\"http://instagram.com\" rel=\"nofollow\">Instagram</a>", "retweeted": false, "coordinates": null, "timestamp_ms": "1435805238190", "entities": {"symbols": [], "user_mentions": [], "trends": [], "hashtags": [{"indices": [0, 10], "text": "Jetlounge"}, {"indices": [12, 29], "text": "100Days100Nights"}, {"indices": [30, 35], "text": "NOLA"}, {"indices": [37, 43], "text": "CWMB3"}], "urls": [{"indices": [44, 67], "url": "https://t.co/0B8c0h1PwS", "expanded_url": "https://instagram.com/p/4nhZSDRV5W/", "display_url": "instagram.com/p/4nhZSDRV5W/"}]}, "in_reply_to_screen_name": null, "in_reply_to_user_id": null, "retweet_count": 0, "id_str": "616437964219023360", "favorited": false, "user": {"follow_request_sent": null, "profile_use_background_image": true, "geo_enabled": true, "description": null, "verified": false, "profile_image_url_https": "https://pbs.twimg.com/profile_images/555269587395026946/agpaj4CS_normal.jpeg", "profile_sidebar_fill_color": "DDEEF6", "is_translator": false, "id": 106863509, "profile_text_color": "333333", "followers_count": 566, "profile_sidebar_border_color": "C0DEED", "id_str": "106863509", "default_profile_image": false, "location": "NEW ORLEANS", "utc_offset": -18000, "statuses_count": 817, "profile_background_color": "C0DEED", "friends_count": 1354, "profile_link_color": "0084B4", "profile_image_url": "http://pbs.twimg.com/profile_images/555269587395026946/agpaj4CS_normal.jpeg", "notifications": null, "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "name": "internationaldaniel", "lang": "en", "profile_background_tile": false, "favourites_count": 211, "screen_name": "DANIELCP3", "url": "http://www.datpiff.com/B3-The-Set-Up-mixtape.687448.html", "created_at": "Wed Jan 20 22:56:10 +0000 2010", "contributors_enabled": false, "time_zone": "Central Time (US & Canada)", "protected": false, "default_profile": true, "following": null, "listed_count": 5}, "geo": null, "in_reply_to_user_id_str": null, "possibly_sensitive": false, "lang": "und", "created_at": "Thu Jul 02 02:47:18 +0000 2015", "filter_level": "low", "in_reply_to_status_id_str": null, "place": null}, "author": {"author_img": "http://pbs.twimg.com/profile_images/555269587395026946/agpaj4CS_normal.jpeg", "name": "internationaldaniel", "url": "http://twitter.com/danielcp3", "nick": "danielcp3", "followers": 555.0, "image_url": "http://pbs.twimg.com/profile_images/555269587395026946/agpaj4CS_normal.jpeg", "type": "twitter", "influence_level": 1.0, "description": ""}, "original_author": {"author_img": "http://pbs.twimg.com/profile_images/555269587395026946/agpaj4CS_normal.jpeg", "description": "", "url": "http://twitter.com/danielcp3", "nick": "danielcp3", "followers": 555.0, "image_url": "http://pbs.twimg.com/profile_images/555269587395026946/agpaj4CS_normal.jpeg", "type": "twitter", "influence_level": 1.0, "name": "internationaldaniel"}, "citation_date": 1435805238, "metrics": {"acceleration": 0, "ranking_score": 8.222051, "citations": {"influential": 1, "total": 2, "data": [{"timestamp": 1435777199, "citations": 0}, {"timestamp": 1435780799, "citations": 0}, {"timestamp": 1435784399, "citations": 0}, {"timestamp": 1435787999, "citations": 0}, {"timestamp": 1435791599, "citations": 0}, {"timestamp": 1435795199, "citations": 0}, {"timestamp": 1435798799, "citations": 0}, {"timestamp": 1435802399, "citations": 0}, {"timestamp": 1435805999, "citations": 0}, {"timestamp": 1435809599, "citations": 0}, {"timestamp": 1435813199, "citations": 0}, {"timestamp": 1435816799, "citations": 0}, {"timestamp": 1435820399, "citations": 0}, {"timestamp": 1435823999, "citations": 0}, {"timestamp": 1435827599, "citations": 0}, {"timestamp": 1435831199, "citations": 0}, {"timestamp": 1435834799, "citations": 0}, {"timestamp": 1435838399, "citations": 0}, {"timestamp": 1435841999, "citations": 0}, {"timestamp": 1435845599, "citations": 0}, {"timestamp": 1435849199, "citations": 0}, {"timestamp": 1435852799, "citations": 0}, {"timestamp": 1435856399, "citations": 0}, {"timestamp": 1435859999, "citations": 0}], "matching": 2, "replies": 0}, "peak": 0, "impressions": 7187, "momentum": 0}, "highlight": "#Jetlounge. #100Days100Nights #NOLA. #CWMB3 https://t.co/0B8c0h1PwS", "type": "tweet", "citation_url": "http://twitter.com/DANIELCP3/status/616437964219023360"}
    {"firstpost_date": 1435806666, "title": "#Jetlounge. #100Days100Nights #NOLA. #CWMB3 by internationalcorporation3 http://t.co/jiibcs21ho http://t.co/Ci2MkoKgMC", "url": "http://twitter.com/instaNewOrleans/status/616443954825981958", "tweet": {"contributors": null, "truncated": false, "text": "#Jetlounge. #100Days100Nights #NOLA. #CWMB3 by internationalcorporation3 http://t.co/jiibcs21ho http://t.co/Ci2MkoKgMC", "in_reply_to_status_id": null, "id": 616443954825981958, "favorite_count": 0, "source": "<a href=\"http://ifttt.com\" rel=\"nofollow\">IFTTT</a>", "retweeted": false, "coordinates": null, "timestamp_ms": "1435806666462", "entities": {"symbols": [], "media": [{"expanded_url": "http://twitter.com/instaNewOrleans/status/616443954825981958/photo/1", "sizes": {"large": {"h": 640, "resize": "fit", "w": 640}, "small": {"h": 340, "resize": "fit", "w": 340}, "medium": {"h": 600, "resize": "fit", "w": 600}, "thumb": {"h": 150, "resize": "crop", "w": 150}}, "url": "http://t.co/Ci2MkoKgMC", "media_url_https": "https://pbs.twimg.com/media/CI4MgsNWcAAO7Ry.jpg", "id_str": "616443954758840320", "indices": [96, 118], "media_url": "http://pbs.twimg.com/media/CI4MgsNWcAAO7Ry.jpg", "type": "photo", "id": 616443954758840320, "display_url": "pic.twitter.com/Ci2MkoKgMC"}], "hashtags": [{"indices": [0, 10], "text": "Jetlounge"}, {"indices": [12, 29], "text": "100Days100Nights"}, {"indices": [30, 35], "text": "NOLA"}, {"indices": [37, 43], "text": "CWMB3"}], "user_mentions": [], "trends": [], "urls": [{"indices": [73, 95], "url": "http://t.co/jiibcs21ho", "expanded_url": "http://ift.tt/1LW6KFA", "display_url": "ift.tt/1LW6KFA"}]}, "in_reply_to_screen_name": null, "in_reply_to_user_id": null, "retweet_count": 0, "id_str": "616443954825981958", "favorited": false, "user": {"follow_request_sent": null, "profile_use_background_image": true, "geo_enabled": false, "description": "Latest pics from New Orleans via Instagram", "verified": false, "profile_image_url_https": "https://pbs.twimg.com/profile_images/551083419187564545/5SaxR6d9_normal.jpeg", "profile_sidebar_fill_color": "DDEEF6", "is_translator": false, "id": 2957041439, "profile_text_color": "333333", "followers_count": 970, "profile_sidebar_border_color": "C0DEED", "id_str": "2957041439", "default_profile_image": false, "location": "", "utc_offset": null, "statuses_count": 154998, "profile_background_color": "C0DEED", "friends_count": 98, "profile_link_color": "0084B4", "profile_image_url": "http://pbs.twimg.com/profile_images/551083419187564545/5SaxR6d9_normal.jpeg", "notifications": null, "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_banner_url": "https://pbs.twimg.com/profile_banners/2957041439/1420223502", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "name": "Pics from NewOrleans", "lang": "ru", "profile_background_tile": false, "favourites_count": 0, "screen_name": "instaNewOrleans", "url": null, "created_at": "Fri Jan 02 18:24:44 +0000 2015", "contributors_enabled": false, "time_zone": null, "protected": false, "default_profile": true, "following": null, "listed_count": 385}, "geo": null, "in_reply_to_user_id_str": null, "possibly_sensitive": false, "lang": "en", "created_at": "Thu Jul 02 03:11:06 +0000 2015", "filter_level": "low", "in_reply_to_status_id_str": null, "place": null, "extended_entities": {"media": [{"expanded_url": "http://twitter.com/instaNewOrleans/status/616443954825981958/photo/1", "sizes": {"large": {"h": 640, "resize": "fit", "w": 640}, "small": {"h": 340, "resize": "fit", "w": 340}, "medium": {"h": 600, "resize": "fit", "w": 600}, "thumb": {"h": 150, "resize": "crop", "w": 150}}, "url": "http://t.co/Ci2MkoKgMC", "media_url_https": "https://pbs.twimg.com/media/CI4MgsNWcAAO7Ry.jpg", "id_str": "616443954758840320", "indices": [96, 118], "media_url": "http://pbs.twimg.com/media/CI4MgsNWcAAO7Ry.jpg", "type": "photo", "id": 616443954758840320, "display_url": "pic.twitter.com/Ci2MkoKgMC"}]}}, "author": {"author_img": "http://pbs.twimg.com/profile_images/551083419187564545/5SaxR6d9_normal.jpeg", "name": "InstaNewOrleans", "url": "http://twitter.com/instaneworleans", "nick": "instaneworleans", "followers": 29.0, "image_url": "http://pbs.twimg.com/profile_images/551083419187564545/5SaxR6d9_normal.jpeg", "type": "twitter", "description": "Latest pics from New Orleans via Instagram"}, "original_author": {"author_img": "http://pbs.twimg.com/profile_images/551083419187564545/5SaxR6d9_normal.jpeg", "description": "Latest pics from New Orleans via Instagram", "url": "http://twitter.com/instaneworleans", "nick": "instaneworleans", "followers": 29.0, "image_url": "http://pbs.twimg.com/profile_images/551083419187564545/5SaxR6d9_normal.jpeg", "type": "twitter", "name": "InstaNewOrleans"}, "citation_date": 1435806666, "metrics": {"acceleration": 48, "ranking_score": 8.218007, "citations": {"influential": 1, "total": 3, "data": [{"timestamp": 1435777199, "citations": 0}, {"timestamp": 1435780799, "citations": 0}, {"timestamp": 1435784399, "citations": 1}, {"timestamp": 1435787999, "citations": 0}, {"timestamp": 1435791599, "citations": 0}, {"timestamp": 1435795199, "citations": 0}, {"timestamp": 1435798799, "citations": 0}, {"timestamp": 1435802399, "citations": 0}, {"timestamp": 1435805999, "citations": 0}, {"timestamp": 1435809599, "citations": 0}, {"timestamp": 1435813199, "citations": 1}, {"timestamp": 1435816799, "citations": 0}, {"timestamp": 1435820399, "citations": 0}, {"timestamp": 1435823999, "citations": 0}, {"timestamp": 1435827599, "citations": 0}, {"timestamp": 1435831199, "citations": 0}, {"timestamp": 1435834799, "citations": 0}, {"timestamp": 1435838399, "citations": 0}, {"timestamp": 1435841999, "citations": 0}, {"timestamp": 1435845599, "citations": 0}, {"timestamp": 1435849199, "citations": 0}, {"timestamp": 1435852799, "citations": 0}, {"timestamp": 1435856399, "citations": 0}, {"timestamp": 1435859999, "citations": 0}], "matching": 3, "replies": 0}, "peak": 1435827599, "impressions": 8377, "momentum": 2}, "highlight": "#Jetlounge. #100Days100Nights #NOLA. #CWMB3 by internationalcorporation3 http://t.co/jiibcs21ho http://t.co/Ci2MkoKgMC", "type": "tweet", "citation_url": "http://twitter.com/instaNewOrleans/status/616443954825981958"}

我尝试解析文本文件的所有方法都给了我这个错误的某些版本:

    JSONDecodeError: Expecting value: line 2 column 1 (char 2)

我当前的代码很简单,但我尝试了几个不同的版本:

    tweets = []
    for line in open('tweets.txt', 'r'):
    tweets.append(json.loads(line))

Utf-8 编码没有修复它,并且我可以看到第 2 行第 1 列没有任何明显的问题。我不知道导致错误的原因,所以我不确定如何尝试修复它。

最佳答案

我认为这可能是由文件末尾的尾随换行符引起的。

for line in open('tweets.txt', 'r'):
    if line.strip():
        tweets.append(json.loads(line))

关于python - 在 Python 中解析 Twitter JSON JSONDecodeError : Expecting value: line 2 column 1 (char 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31975395/

相关文章:

python - 使用 Python 从 Twitter API 获取历史推文元数据

python - 如何使 sklearn.metrics.confusion_matrix() 始终返回 TP、TN、FP、FN?

javascript - Javascript 的 JSON.parse 有问题吗?

javascript - jQuery.getJSON - 访问控制允许来源问题

javascript - 从 JS 对象到 JSON Angular

java - 在 Twitter/Apache Storm 中共享资源

python - 无法在 python 3.4 上发布推文

python - 如何在本地 Django 环境中提供媒体文件?

python - 从包导入时模块抛出名称错误

python - 如何组织数据库访问层?