首页 > 解决方案 > 打开 JSON txt 文件并将其设置为 DataFrame 时发生 KeyError

问题描述

我有一个代码,它给了我一个没有保存推文的空 DataFrame。我试图通过print(line)在 json 文件中的 for 行下进行调试:和json_data = json.loads(line). 这导致了一个KeyError. 我如何解决它?谢谢你。

list_df = list()
# read the .txt file, line by line, and append the json data in each line to the list
with open('tweet_json.txt', 'r') as json_file:
    for line in json_file:
        print(line)
        json_data = json.loads(line)
        print(line)
        tweet_id = json_data['tweet_id']
        fvrt_count = json_data['favorite_count']
        rtwt_count = json_data['retweet_count']
        list_df.append({'tweet_id': tweet_id,
                        'favorite_count': fvrt_count,
                        'retweet_count': rtwt_count})

# create a pandas DataFrame using the list
df = pd.DataFrame(list_df, columns = ['tweet_id', 'favorite_count', 'retweet_count'])
df.head()

标签: pythonjsonpandastwitterkeyerror

解决方案


您的评论说您正在尝试保存到文件,但您的代码有点说您正在尝试从文件中读取。以下是如何做到这两点的示例:

写入 JSON

import json
import pandas as pd

content = {  # This just dummy data, in the form of a dictionary
    "tweet1": {
        "id": 1,
        "msg": "Yay, first!"
    },
    "tweet2": {
        "id": 2,
        "msg": "I'm always second :("
    }
}
# Write it to a file called "tweet_json.txt" in JSON
with open("tweet_json.txt", "w") as json_file:
    json.dump(content, json_file, indent=4)  # indent=4 is optional, it makes it easier to read

注意w(如)中的open("tweet_json.txt", "w")。您正在使用r(如在阅读中),它不允许您写任何东西。还要注意使用json.dump()而不是json.load()。然后我们得到一个看起来像这样的文件:

$ cat tweet_json.txt
{
    "tweet1": {
        "id": 1,
        "msg": "Yay, first!"
    },
    "tweet2": {
        "id": 2,
        "msg": "I'm always second :("
    }
}

从 JSON 读取

让我们使用 pandas 读取我们刚刚编写的文件read_json()

import pandas as pd

df = pd.read_json("tweet_json.txt")
print(df)

输出如下所示:

>>> df
          tweet1                tweet2
id             1                     2
msg  Yay, first!  I'm always second :(

推荐阅读