gpt4 book ai didi

python - 如何将 Twitter json 对象加载到 python 中

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:19 24 4
gpt4 key购买 nike

我想将从 twitter api 中提取的 json 加载到 python 中。附件是 json 对象的示例:

{"created_at":"Mon Apr 22 18:17:09 +0000 2019","id":1120391103813910529,"id_str":"1120391103813910529","text":"On peut dire que la base de cette 8e saison est en place \ud83d\ude4c #GOTS8E2","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":243071138,"id_str":"243071138","name":"Mr B","screen_name":"skeyos","location":"Namur","url":null,"description":null,"translator_type":"none","protected":false,"verified":false,"followers_count":197,"friends_count":1811,"listed_count":6,"favourites_count":7826,"statuses_count":8044,"created_at":"Wed Jan 26 06:49:05 +0000 2011","utc_offset":null,"time_zone":null,"geo_enabled":true,"lang":"fr","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/493833348167770112\/aGLGemZ5_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/493833348167770112\/aGLGemZ5_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/243071138\/1406574068","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"GOTS8E2","indices":[59,67]}],"urls":[],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"fr","timestamp_ms":"1555957029666"}

{"created_at":"Mon Apr 22 18:17:14 +0000 2019","id":1120391124722565123,"id_str":"1120391124722565123","text":"...

我正在尝试以下代码:

with open('tweets.json') as tweet_data:
json_data = json.load(tweet_data)

但是得到如下错误:

JSONDecodeError: Extra data: line 3 column 1 (char 2149)

不幸的是,我无法过多地编辑 json 对象,因为它确实很大。我需要弄清楚如何将其读入 Python。任何帮助将不胜感激!

编辑:它适用于以下代码:

dat=list()
with open ('data_tweets_E2.json', 'r') as f:
for l in f.readlines():
if not l.strip (): # skip empty lines
continue

json_data = json.loads (l)
dat.append(json_data)

最佳答案

这是代码。当然你需要先安装Pandas。如果解决方案对您有帮助,请用绿色勾选此答案。

import json
import pandas as pd

with open('tweets.json') as json_file:
data_list = json.load(json_file)

tweet_data_frame = pd.DataFrame.from_dict(data_list)
print(tweet_data_frame)
print(data_list)

如您所见,print(data_list) 打印出一个列表,print(tweet_data_frame) 打印出数据框。

如果您想查看这些变量的类型,只需使用 type() print(type(data_list))

重要提示:我想告诉您的是,您的 JSON 文件格式不正确且有很多错误。如果你有更多的 JSON 对象,它们需要在数组 [{"example":"value"},{"example":"value"}] 中。您的 JSON 文件有错误。尝试使用不同的 JSON 文件。

关于python - 如何将 Twitter json 对象加载到 python 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55929752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com