将大的illformatted.json文件转换为cs

2024-10-03 06:19:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我很少使用python和.json文件。我想把从别人那里收到的一个大的.json文件转换成一个.csv文件在excel中使用。在

文件的格式如下:

{
"bedrock": {
    "basinAge": "",
    "basinName": "",
    "basinSetting": "",
    "basinSource": "",
    "basinType": "",
    "division": "ATLANTICPLAIN",
    "primary": "ATLANTICPLAIN",
    "province": "COASTALPLAIN",
    "section": "MISSISSIPPIALLUVIALPLAIN"
},
"country": "US",
"county": "ButlerCounty",
"crow": {
    "PERIOD_RAN": "",
    "SITE_PERIO": "",
    "SURFACE_EL": "",
    "VS30_RANGE": "",
    "ZDRIFT": "",
    "ZONE": "",
    "ZPLEIS": "",
    "Zhol": "",
    "condition": "",
    "firmThickness": "",
    "geobed": "",
    "geodes": "",
    "geophone": "",
    "meas_type": "",
    "resonance": "",
    "sitelocation": "",
    "sitenumber": "",
    "sitevs30": "",
    "slope": "",
    "slopevel": "",
    "soilThickness": "",
    "veltofirm": "",
    "vs30": ""
},
"embaymentDepth": 27.176477284750096,
"file": "../../data\\anderson\\anderson-et-al-2003-MoDOT.json",
"geologicClass": "YNa",
"geology": "al",
"geologySource": "fullerton",
"lat": 36.790518,
"latlon": [
    [
        "36.7905",
        "-90.2025"
    ],
    "232.0000",
    0.00172447,
    "stable"
],
"location": "BridgeA-3709",
"lon": -90.202518,
"profile": {
    "entry": {
        "0": [
            0,
            146.185,
            "Empty"
        ],
        "1": [
            2.91874,
            194.378,
            "Empty"
        ],
        "2": [
            4.11277,
            228.112,
            "Empty"
        ],
        "3": [
            6.10282,
            221.687,
            "Empty"
        ],
        "4": [
            7.9602,
            221.687,
            "Empty"
        ],
        "5": [
            8.09287,
            220.08,
            "Empty"
        ],
        "6": [
            8.09287,
            216.867,
            "Empty"
        ],
        "7": [
            14.063,
            260.241,
            "Empty"
        ],
        "8": [
            18.0431,
            279.518,
            "Empty"
        ],
        "9": [
            22.1559,
            282.731,
            "Empty"
        ],
        "10": [
            26.0033,
            281.124,
            "Empty"
        ],
        "11": [
            29.9834,
            276.305,
            "Empty"
        ],
        "12": [
            36.0862,
            293.976,
            "Empty"
        ],
        "13": [
            41.9237,
            435.341,
            "Empty"
        ],
        "14": [
            48.0265,
            557.43,
            "Empty"
        ],
        "15": [
            54.1294,
            640.964,
            "Empty"
        ],
        "16": [
            59.8342,
            726.104,
            "Empty"
        ],
        "17": [
            68.1924,
            "Empty",
            "Empty"
        ]
    },
    "units": [
        "m",
        "m/s",
        "m/s"
    ]
},
"sedEnd": "",
"sedStack": "",
"sedStart": "",
"sedSubsurface": "",
"sedSurficial": "",
"sedVaneer": "",
"site": "SASW",
"state": "MO",
"terrain": "16",
"terrainvel": "246",
"vs30": {
    "profileListed": {
        "units": "",
        "value": "None"
    },
    "siteListed": {},
    "stationlisted": {
        "method": "",
        "units": "",
        "value": ""
    },
    "units": "m/s",
    "value": 232.2477304197259,
    "wald": "",
    "yong": ""
},
"vsz": [
    146.185,
    146.185,
    147.1733748014587,
    156.68616932663514,
    166.6758658515508,
    174.5091277144315,
    180.04135355419726,
    184.37079145300547,
    187.52878874899267,
    190.10050728694824,
    192.25770054815115,
    194.09311720243602,
    195.6737567374426,
    197.04922523230243,
    200.16214171750644,
    203.0924940564577,
    205.75028418598887,
    208.17185007705515,
    210.97976183739868,
    213.5984956154859,
    216.02447901610586,
    218.27823757348278,
    220.44997122220872,
    222.4921122273311,
    224.40458487503645,
    226.19935932262254,
    227.84822291575128,
    229.40085613024158,
    230.86555428020594,
    232.2477304197259,
    265.0897348970574,
    "",
    "",
    "",
    "",
    "",
    ""
]
}

像上面这样的条目有1000个,每个条目都有相同的键。在网上做了一些研究之后,我很确定我需要把条目放平,但不知道如何用程序来实现。有些分类指标后面有一系列的关键字(‘基岩’、‘乌鸦’等),必要时可以丢弃。在


Tags: 文件csvjsonvalue格式条目excelanderson
1条回答
网友
1楼 · 发布于 2024-10-03 06:19:42

第一步肯定是用JSON解析器解析文件。然后编写代码查看生成的字典并提取数据。在

我不知道您所说的“格式错误”是什么意思;它看起来像是有效的JSON。如果在使用Python的json模块解析它时遇到问题,可以尝试用Python的yaml模块来处理它。YAML是JSON的超集,但它更能容忍一些小的格式设置,比如不需要的逗号。在

http://pymotw.com/2/json/

https://pypi.python.org/pypi/PyYAML

相关问题 更多 >