Python 脚本 json 到 csv

问题描述 投票:0回答:1

美好的一天,

我对Python脚本知之甚少。我正在尝试获取一个脚本来帮助我将 json 文件转换为 csv。我尝试了许多脚本,但不断遇到错误,而且没有足够的知识来修复错误。

我只需要有人帮助我编写一个脚本,可以将以下内容转换为 csv。源json中有几千条记录。

有人可以帮忙吗?

{
    "meterList": [
        {
            "installID": 344,
            "meterKeyRef": "06B1008368_42615",`
            "meterCode": "06B1008368",
            "meterTypeCode": "5020ELS",
            "meterTypeName": "5 Dial 20mm Elster",
            "tagCodeFriendly": "307890",
            "rateCode": "RuralPipe",
            "activatedTimestamp": "2015-12-10 11:00:00 +10:00",
            "deactivatedTimestamp": null,
            "runNumber": "888608",
            "runSequenceNumber": 125,
            "meterStatusCode": "Active",`your text`
            "meterHazard1": null,
            "meterHazard2": null,
            "meterNotes": null,
            "locationNotes": "METER IS 06B1008368",
            "serialNumber": null,
            "meterLatitude": -36.6763,
            "meterLongitude": 142.243,
            "meterDigits": 0,
            "meterInletMaterial": null,
            "meterInletSize": "20",
            "waterOffTimestamp": null,
            "waterOnTimestamp": null,
            "initialMeterRead": 0,
            "finalMeterRead": null,
            "meterLocationCode": null,
            "propertyKeyRef": "108275",
            "masterFlag": false,
            "deductiveFlag": false,
            "slaveFlag": false,
            "sourceCode": "DLT",
            "referenceSystemSyncTimestamp": null,
            "networkPrivateFlag": false,
            "networkMeterFlag": false,
            "schemeCode": null,
            "clientLocationTitle": null,
            "clientLocationPosition": null,
            "excludeFromPathwaySync": false,
            "ignoreLeaksBelow": 0,
            "propertyID": 6325,
            "modelID": 4,
            "compoundMeterInstallID": null,
            "tradeWasteMeterFlag": false,
            "tradeWasteMeterType": null,
            "complexBillingFlag": false,
            "complexBillingComments": null,
            "fireMeterFlag": false,`your text`
            "meterPositionCode": null,
            "asseticAssetID": null,
            "connectionID": null,
            "allocationPercentage": 100
        },

我尝试了许多预先创建的脚本,但遇到了各种各样的错误。我只是没有足够的知识来正确解决它们。

有人可以根据我的 json 文件中的信息帮助编写一个脚本吗?

感谢任何帮助。

python json csv
1个回答
0
投票

这是将 json 转换为 csv 的基本脚本。

您可以动态更改代码中的数据。

 import json
 import csv

 data = {
 "meterList": 
         [
            {
                "installID": 344,
                "meterKeyRef": "06B1008368_42615",
                "meterCode": "06B1008368",
                "meterTypeCode": "5020ELS",
                "meterTypeName": "5 Dial 20mm Elster",
                "tagCodeFriendly": "307890",
                "rateCode": "RuralPipe",
                "activatedTimestamp": "2015-12-10 11:00:00 +10:00",
                "deactivatedTimestamp": None,
                "runNumber": "888608",
                "runSequenceNumber": 125,
                "meterStatusCode": "Active",
                "meterHazard1": None,
                "meterHazard2": None,
                "meterNotes": None,
                "locationNotes": "METER IS 06B1008368",
                "serialNumber": None,
                "meterLatitude": -36.6763,
                "meterLongitude": 142.243,
                "meterDigits": 0,
                "meterInletMaterial": None,
                "meterInletSize": "20",
                "waterOffTimestamp": None,
                "waterOnTimestamp": None,
                "initialMeterRead": 0,
                "finalMeterRead": None,
                "meterLocationCode": None,
                "propertyKeyRef": "108275",
                "masterFlag": False,
                "deductiveFlag": False,
                "slaveFlag": False,
                "sourceCode": "DLT",
                "referenceSystemSyncTimestamp": None,
                "networkPrivateFlag": False,
                "networkMeterFlag": False,
                "schemeCode": None,
                "clientLocationTitle": None,
                "clientLocationPosition": None,
                "excludeFromPathwaySync": False,
                "ignoreLeaksBelow": 0,
                "propertyID": 6325,
                "modelID": 4,
                "compoundMeterInstallID": None,
                "tradeWasteMeterFlag": False,
                "tradeWasteMeterType": None,
                "complexBillingFlag": False,
                "complexBillingComments": None,
                "fireMeterFlag": False,
                "meterPositionCode": None,
                "asseticAssetID": None,
                "connectionID": None,
                "allocationPercentage": 100
            }
         ]
        }


    csv_file = "meter_data.csv"

    with open(csv_file, mode='w', newline='') as file:
        writer = csv.DictWriter(file, fieldnames=data['meterList'][0].keys())
        writer.writeheader()
        writer.writerows(data['meterList'])

    print(f"JSON data has been converted to {csv_file}")
    
© www.soinside.com 2019 - 2024. All rights reserved.