Python 3.x 使用Python将JSON文件转换为CSV文件

Python 3.x 使用Python将JSON文件转换为CSV文件,python-3.x,Python 3.x,我有以下JSON文件: { "took": 17, "timed_out": false, "_shards": { "total": 25, "successful": 25, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [

我有以下JSON文件:

    {
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 25,
        "successful": 25,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
            {
                "_index": "w106941",
                "_type": "wqr",
                "_id": "51929",
                "_score": 1,
                "_source": {
                    "tstamp": "1978-12-06T23:00:00Z",
                    "value": 41,
                    "name": "SO4-Dis",
                }
            },
            {
                "_index": "w6406931",
                "_type": "wqr",
                "_id": "51929",
                "_score": 1,
                "_source": {
                    "tstamp": "1979-12-11T23:00:00Z",
                    "value": 45,
                    "name": "SO4",
                }
            },
            {
                "_index": "w2049185",
                "_type": "wqr",
                "_id": "51929",
                "_score": 1,
                "_source": {
                    "tstamp": "1980-12-22T23:00:00Z",
                    "value": 48,
                    "name": "T-D2",
                }
            }
        ]
    }
}
我想在python中创建具有以下格式的
csv

tstamp;value;name;

1978-12-06T23:00:00Z;41;SO4-Dis;

1979-12-11T23:00:00Z;45;SO4;

1980-12-22T23:00:00Z;48;T-D2;
我尝试了这个页面中的所有代码。 我还尝试了
df

df = pd.read_json("example.json")
df_hits=df['hits']
#df = df.loc[["sta_no_s", "wqtr_tstamp_ts", "part_shortname_s"]]
df_hits.to_csv("sample.csv", index=False, sep=';')
f = csv.writer(open("test.csv", "w+"))

# Write CSV Header, If you dont need that, remove this line
f.writerow(["tstamp", "value", "name"])


for x in df:
    f.writerow([df["hits"]["tstamp"],
            df["hits"]["value"],
            df["hits"]["name"]])
sample.close()

sample.close()

result = response.json()['hits']
response.json()
result_hits = result['hits']
# open a file for writing
sample = open('sample.csv', 'w')
# create the csv writer object without \n

csvwriter = csv.writer(sample,delimiter=';',lineterminator='\n',)

count = 0


for x in result_hits:
source=x['_source']
print(source)
if count == 0:
         header = source.keys()
         csvwriter.writerow(header)
         count += 1
  csvwriter.writerow(source.values())