【问题标题】:How to parse specific parts of nested JSON format into csv in python (pandas)如何在python(熊猫)中将嵌套JSON格式的特定部分解析为csv
【发布时间】:2020-01-22 14:24:30
【问题描述】:

我有一个嵌套的 JSON 文件,我无法将其解析为扁平化的 csv。 我想在 csv 中有以下列: id、名称、路径、标签(每个都有一列)、点(我需要 4 个点的 x\y 值)

JSON 输入示例:

{
"name": "test",
"securityToken": "test Token",
"videoSettings": {
    "frameExtractionRate": 15
},
"tags": [
    {
        "name": "Blur Reject",
        "color": "#FF0000"
    },
    {
        "name": "Blur Poor",
        "color": "#800000"
    }
],
"id": "Du1qtrZQ1",
"activeLearningSettings": {
    "autoDetect": false,
    "predictTag": true,
    "modelPathType": "coco"
},
"version": "2.1.0",
"lastVisitedAssetId": "ddee3e694ec299432fed9e42de8741ad",
"assets": {
    "0b8f6f214dc7066b00b50ae16cf25cf6": {
        "asset": {
            "format": "jpg",
            "id": "0b8f6f214dc7066b00b50ae16cf25cf6",
            "name": "1.jpg",
            "path": "c:\temp\1.jpg",
            "size": {
                "width": 1500,
                "height": 1125
            },
            "state": 2,
            "type": 1
        },
        "regions": [
            {
                "id": "VtDyR9Ovl",
                "type": "POLYGON",
                "tags": [
                    "3",
                    "9",
                    "Dark Poor"
                ],
                "boundingBox": {
                    "height": 695.2110389610389,
                    "width": 1111.607142857143,
                    "left": 167.41071428571428,
                    "top": 241.07142857142856
                },
                "points": [
                    {
                        "x": 167.41071428571428,
                        "y": 252.02922077922076
                    },
                    {
                        "x": 208.80681818181816,
                        "y": 891.2337662337662
                    },
                    {
                        "x": 1252.232142857143,
                        "y": 936.2824675324675
                    },
                    {
                        "x": 1279.017857142857,
                        "y": 241.07142857142856
                    }
                ]
            }
        ],
        "version": "2.1.0"
    },
    "0155d8143c8cad85b5b9d392fd2895a4": {
        "asset": {
            "format": "jpg",
            "id": "0155d8143c8cad85b5b9d392fd2895a4",
            "name": "2.jpg",
            "path": "c:\temp\2.jpg",
            "size": {
                "width": 1080,
                "height": 1920
            },
            "state": 2,
            "type": 1
        },
        "regions": [
            {
                "id": "7FFl_diM2",
                "type": "POLYGON",
                "tags": [
                    "Dark Poor"
                ],
                "boundingBox": {
                    "height": 502.85714285714283,
                    "width": 820.3846153846155,
                    "left": 144.08653846153848,
                    "top": 299.2207792207792
                },
                "points": [
                    {
                        "x": 152.39423076923077,
                        "y": 311.68831168831167
                    },
                    {
                        "x": 144.08653846153848,
                        "y": 802.077922077922
                    },
                    {
                        "x": 964.4711538461539,
                        "y": 781.2987012987012
                    },
                    {
                        "x": 935.3942307692308,
                        "y": 299.2207792207792
                    }
                ]
            }
        ],
        "version": "2.1.0"
    }

}

我尝试使用 pandas 的 json_normalize 并意识到我不完全了解如何指定我希望解析的列:

import json
import csv
import pandas as pd
from pandas import Series, DataFrame
from pandas.io.json import json_normalize 


f = open(r'c:\temp\test-export.json') 
data = json.load(f) # load as json
f.close()
df = json_normalize(data) #load json into dataframe
df.to_csv(r'c:\temp\json-to-csv.csv', sep=',', encoding='utf-8') 

结果很难处理,因为我没有指定我想要的(遍历特定数组并将其附加到 CSV) 我希望你的帮助。

我假设我不完全了解规范化的工作原理,并怀疑它不是处理此问题的最佳方法。

谢谢!

【问题讨论】:

  • 您还应该根据您提供的数据提供示例输出。此外,您应该展示您已经尝试过的内容以及为什么它不起作用。如果您遇到困难,我们会在这里为您提供帮助,但不是为您解决问题。表现出自己解决它的一些努力。请参阅tourHow to Ask 了解如何提出好问题的更多信息。
  • 是的,我明白了。我将编辑问题并提供更多信息
  • @AlexanderPushkarev 当他只需要 CSV 中未嵌套的部分 JSON 文件时,他为什么要在 CSV 中表示嵌套的 JSON。

标签: python json csv parsing


【解决方案1】:

你可以做这样的事情。由于您没有提供示例输出,我自己做了一些事情。

import json
import csv

f = open(r'file.txt')
data = json.load(f)
f.close()
with open("output.csv", mode="w", newline='') as out:
    w = csv.writer(out)
    header = ["id","name","path","tags","points"]
    w.writerow(header)
    for asset in data["assets"]:
        data_point = data["assets"][asset]
        output = [data_point["asset"]["id"]]
        output.append(data_point["asset"]["name"])
        output.append(data_point["asset"]["path"])
        output.append(data_point["regions"][0]["tags"])
        output.append(data_point["regions"][0]["points"])
        w.writerow(output)

输出

id,name,path,tags,points
0b8f6f214dc7066b00b50ae16cf25cf6,1.jpg,c:\temp\1.jpg,"['3', '9', 'Dark Poor']","[{'x': 167.41071428571428, 'y': 252.02922077922076}, {'x': 208.80681818181816, 'y': 891.2337662337662}, {'x': 1252.232142857143, 'y': 936.2824675324675}, {'x': 1279.017857142857, 'y': 241.07142857142856}]"
0155d8143c8cad85b5b9d392fd2895a4,2.jpg,c:\temp\2.jpg,['Dark Poor'],"[{'x': 152.39423076923077, 'y': 311.68831168831167}, {'x': 144.08653846153848, 'y': 802.077922077922}, {'x': 964.4711538461539, 'y': 781.2987012987012}, {'x': 935.3942307692308, 'y': 299.2207792207792}]"

【讨论】:

  • 这很有帮助!我现在明白了,我正在寻找一个非常远的解决方案(标准化和展平),而您的解决方案要整洁得多。
  • @jonny3000 不客气。请注意,区域是一个数组,在您的示例中它只有一个元素,这就是我将 [0] 放在那里的原因。但如果有更多元素,您必须正确解决该问题。但我想,既然你谈到了 4 点,它可能总是只有 1 的大小?是的,扁平化和标准化可能不是正确的方法。
猜你喜欢
  • 1970-01-01
  • 2022-01-19
  • 2021-08-31
  • 1970-01-01
  • 2021-10-29
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多