我有一个代码,它给了我一个空的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()
您的评论说您正在尝试保存到文件,但是您的代码有点说您正在尝试从文件读取。以下是如何同时执行这两个示例:
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")
(如w仪式)。您正在使用r
(如r ead),这没有授予您编写任何内容的权限。还请注意使用json.dump()
而不是json.load()
。然后,我们得到一个看起来像这样的文件:
$ cat tweet_json.txt
{
"tweet1": {
"id": 1,
"msg": "Yay, first!"
},
"tweet2": {
"id": 2,
"msg": "I'm always second :("
}
}
让我们使用熊猫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 :(