将json数据读入DataFrame

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

我遇到了一些json文件(从Twython / Twitter API生成)的问题。

该文件如下所示:

[
{
    "created_at": "Thu Mar 14 20:24:53 +0000 2019",
    "id": 1106290123426140165,
    "id_str": "1106290123426140165",
    "text": "RT @ALABDULLATIF: n@B_Al3bdullatif \n\u278b\u2026",
    "source": "<a href=\"http://twitter.com/download/android\" 
     rel=\"nofollow\">Twitter for Android</a>",
    "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": 1091414851400929286,
        "id_str": "1091414851400929286",
        "name": "u064a",
        "screen_name": "UThbZ4nwsuzAMQm",
        "location": null,
        "url": null,
        "description": null,
        "translator_type": "none",
        "protected": false,
        "verified": false,
        "followers_count": 0,
        "friends_count": 0,
        "listed_count": 0,
        "favourites_count": 0,
        "statuses_count": 2,
        "created_at": "Fri Feb 01 19:15:52 +0000 2019",
        "utc_offset": null,
        "time_zone": null,
        "geo_enabled": false,
        "lang": "en",
        "contributors_enabled": false,
        "is_translator": false,
        "profile_background_color": "F5F8FA",
        ETC

当我尝试用它读它时:

fname = "tweets_03.json" 

text=[]
retweets=[]
language=[]
followers=[]

with open(fname, 'r') as f:
    for line in f:
        if not line.isspace():
            tweet = json.loads(line)
            text.append(tweet.get('text', ''))
            retweets.append(tweet.get('retweet_count',''))
            language.append(tweet.get('lang',''))
            followers.append(tweet.get('followers_count',''))

text=pd.DataFrame(text)
text.columns=['text']
retweets=pd.DataFrame(retweets)
retweets.columns=['retweets']
language=pd.DataFrame(language)
language.columns=['language']
followers=pd.DataFrame(followers)
followers.columns=['followers']

df=pd.concat([text,retweets,language,followers],axis=1)
df.head(5)

我收到以下错误信息:

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

我也尝试过:

data = "tweets_03.json" 
jdata = json.loads(data)
df = pd.DataFrame(jdata)

这给了我以下错误:

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

如果有人可以帮助它,将非常感激。我想将数据转换为数据帧。谢谢你的祝福

python json dataframe twitter
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.