如何从JSON写入CSV格式,而行中的行键集合都不相同

时间:2019-04-28 16:39:51

标签: json python-3.x export-to-csv

我正在使用Python脚本触发API来获取JSON内容并将其另存为CSV。但是,数据正在写入错误的列。我发现JSON中的数据并不总是具有相同的键。结果是数据被写入CSV文件中的错误列。

我试图显式定义键,但是遇到同样的问题。我确定我犯了一些非常基本的错误。

import json
import requests
import csv

def get_data():
    group_id = 9039
    api_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    api_url = 'https://api.samsara.com/v1'
    endpoint_url = api_url + '/fleet/drivers'

    my_params = {"access_token": api_token}
    my_data = {"groupId": group_id}
    resp = requests.post(url = endpoint_url, params = my_params, json = my_data)
    array = resp.json()
    text = json.dumps(array)

    return text


def write_file(filename, text):
    dataset = json.loads(text)
    drivers = dataset['drivers']

    csvFile = open(filename,'w')
    csvwriter = csv.writer(csvFile)

    # write header
    driver_keys = ["id","groupId","vehicleId","currentVehicleId","username"]
    for d in drivers:
        for key in d.keys():
            if key not in driver_keys:
                driver_keys.append(key)
    csvwriter.writerow(driver_keys)

    # write data
    for line in drivers:
        for key in driver_keys:
            if key not in line.keys():
                line[key] = None #line[key] = '' if you like
        csvwriter.writerow(line.values())

    csvFile.close()

text = get_data()
write_file('drivers.csv', text)

这里是显示问题的部分输出。请注意,如果未指定vehicleId或currentVehicleID,我想要一个NULL或''值。

id,groupId,vehicleId,currentVehicleId,username
321680,9039,,212014918234501,awinchell
91907,9039,,abrauer
134763,9039,212014918234742,abarbosa
134767,9039,212014918234707,212014918234707,ctoepfer

这就是我想要的。

id,groupId,vehicleId,currentVehicleId,username
321680,9039,,212014918234501,awinchell
91907,9039,,,abrauer
134763,9039,,212014918234742,abarbosa
134767,9039,212014918234707,212014918234707,ctoepfer

0 个答案:

没有答案