1

我有一个 JSON 文件data.json,其中包含大型高音扬声器记录。我正在尝试将此 JSON 文件加载到 Jupyter,然后传输到 pandas 数据框以进行进一步分析。到目前为止,我已经编写了以下代码:推文示例是'{"_id":{"$oid":"5ec248611c9b498cdbf095a1"},"created_at":"Mon Dec 31 23:19:39 +0000 2018","id":{"$numberLong":"1079879790738325504"},"id_str":"1079879790738325504","text":"NPAF's Artist in Residence, Composer Glenn McClure is at the Park at work on his unusual sonification compostions

import json
import csv
json_file = "\\Users\\data.json"
header = ["id_str", "created_at", "lang", "text"]
tweets_processed = 0
with open(json_file, 'r') as infile:
    print("json file: ", json_file)
    
    for line in infile:
        tweet = json.loads(line)
        
        #row = [tweet['id_str'], tweet['created_at'], tweet['lang'], tweet['text']]
        
       #csvwriter.writerow(row)
        
        tweets_processed += 1
        
#print("tweet processed: ", tweet_processed)

到目前为止,这是我编写的代码,基本上是为了读取我的 json 文件并将其传递给 pandas 数据框。关于如何将我的 json 数据放入 pandas 数据框的任何帮助?提前致谢。

4

2 回答 2

1

您从未将名称 csvwriter name 导入命名空间。

在这种情况下,您可能应该使用 csv.writer.writerow()。或者,如果您尝试使用 csvwriter 包(我怀疑您是否正在尝试这样做,那么您需要添加import csvwriter到文件顶部。

要点是阅读您尝试使用的包的文档并将所有内容导入正确的命名空间。

于 2021-08-17T16:33:25.487 回答
0

最简单的方法是使用 pandas 自己的函数 read_json()。

import pandas as pd

dataset = pd.read_json('file.json')

在此处输入图像描述

文件必须包含这样的格式

在此处输入图像描述

于 2021-08-17T16:34:27.110 回答