Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Twitter json对象加载到python中_Python_Json - Fatal编程技术网

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

如何将Twitter json对象加载到python中,python,json,Python,Json,我想将从twitterapi挖掘的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":"\u0

我想将从twitterapi挖掘的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)
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)
但会出现以下错误:

JSONDecodeError: Extra data: line 3 column 1 (char 2149)
不幸的是,我不可能过多地编辑json对象,因为它确实很大。我需要弄清楚如何将其读入Python。任何帮助都将不胜感激

编辑:它使用以下代码:

with open('tweets.json') as tweet_data:
    json_data = json.load(tweet_data)
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)

每一行都包含一个新对象,所以请尝试逐行解析它们

导入json
将open('tweets.json','r')作为f:
对于f.readlines()中的l:
如果不是l.strip():#跳过空行
持续
json_data=json.loads(l)
打印(json_数据)

每行包含一个单独的json对象,解析它们并将其存储到列表中:

with open('tweets.json', 'r') as tweet_data:
    values = [json.loads(line) for line in tweet_data.readlines() 
              if not line.strip()]

这是代码。当然,您需要先安装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\u list)
打印出一个列表,
print(tweet\u data\u frame)
打印出数据帧

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


重要提示:我想告诉您的是,您的JSON文件格式不好,并且有很多错误。如果您有更多的JSON对象,它们需要位于数组
[{“示例”:“值”},{“示例”:“值”}]
中。您的JSON文件有错误。尝试使用不同的JSON文件

每一行都包含一个新对象,所以请尝试逐行解析它们。另外,如果要从字符串进行解析,请使用
加载
。请尝试使用下面的我的代码,我还帮助您使用数据帧。您得到的错误是由于json文件的语法错误造成的。但代码是正确的。因此,请尝试1个json对象并检查您的错误。感谢它已加载。虽然当我键入json_数据时,只会显示最后一条tweet。如何将所有tweet存储在一个对象中?如果我能将json格式转换成数据帧,那就太好了!每次迭代,
json_data
存储一个对象。如果你想访问它们的话,就把它们放到一个列表或别的什么东西里。嗨,唯一的问题是我现在有一个列表而不是一本字典(见问题中的编辑)。有没有办法通过列表来实现这一点?或者,如果我可以在第一时间将数据附加到一个字典中,这也可以,但我不知道如何操作。我尝试了您的代码,但遇到了相同的JSONDECODE错误。这就是我将所有数据添加到列表中的原因。如果有一种方法可以将所有数据加载到字典中而不是列表中,那么这对我来说是可行的too@ArtTatum好的,我编辑了我的代码。尝试使用不同的JSON文件,您会发现文件已损坏。希望能有帮助。请让我知道这是否是您要找的。:)谢谢,我明白你的意思了。幸运的是,我不需要那个些包含坏数据的列。