如何在Python3中导入带有推特ID的CSV / JSON文件?

问题描述 投票:1回答:1

我想使用Python 3在Jupiter Notebook中导入具有所有tweet ID的JSON或CSV文件。我尝试过

    import json

    with open('/path/namefile') as json_file:
        data = json.load(json_file)
        print(data)

但是我收到以下错误:JSONDecodeError: Expecting value: line 1 column 1 (char 0)。不幸的是,当我尝试使用文本打开文件JSON时,我的计算机运行缓慢,并且无法在最后打开文件,因此无法看到需要考虑的列。但是,列应为以下内容:

coordinates,created_at,hashtags,media,urls,favorite_count,id,in_reply_to_screen_name,in_reply_to_status_id,in_reply_to_user_id,lang,place,possibly_sensitive,retweet_count,reweet_id,retweet_screen_name,source,text,tweet_url,user_created_at,user_screen_name,user_default_profile_image,user_description,user_favourites_count,user_followers_count,user_friends_count,user_listed_count,user_location,user_name,user_screen_name,user_statuses_count,user_time_zone,user_urls,user_verified

您能否告诉我我做错了什么以及如何导入这些ID?非常感谢

python-3.x
1个回答
0
投票

如果不查看该输入文件的内容,则只能猜测可能是什么问题。

由于json库在文件(line 1 column 1 (char 0))的开头处引发错误,所以它可能不是JSON文件(可能是CSV)。

如果无法在系统中将其作为文本文件打开(文件有多大?),则可以使用Python读取文件中的前几个字符以弄清数据的性质。

with open('/path/to/file', 'r') as f:
    print(f.read(size=100))

这将打印文件的前100个字符,这将使您根据数据的性质了解如何继续操作。如果是CSV文件,则在打印输出中会有一堆逗号。

© www.soinside.com 2019 - 2024. All rights reserved.