【问题标题】:Process JSON data from mediainfo output处理来自 mediainfo 输出的 JSON 数据
【发布时间】:2021-09-09 07:17:24
【问题描述】:

我正在从子进程运行“MediaInfo”命令:

cmd = ["mediainfo", "--output=JSON", file_loc]

我得到的输出是:

{
"media": {
"@ref": "/home/mediaworker/divergent.jpg",
"track": [
{
"@type": "General",
"ImageCount": "1",
"FileExtension": "jpg",
"Format": "JPEG",
"FileSize": "84227",
"StreamSize": "0",
"File_Modified_Date": "UTC 2019-07-16 05:36:32",
"File_Modified_Date_Local": "2019-07-16 11:06:32"
},
{
"@type": "Image",
"Format": "JPEG",
"Width": "612",
"Height": "612",
"ColorSpace": "YUV",
"ChromaSubsampling": "4:4:4",
"BitDepth": "8",
"Compression_Mode": "Lossy",
"StreamSize": "84227"
}
]
}
}

我正在尝试以不同的方式重新格式化此数据,如下所示: 基本上维护一个字典列表。

[
{
"desc":"ImageCount",
"val" : "1"
},
{
"desc":"FileExtension",
"val" : "jpg"
},
{
"desc":"Format",
"val" : "JPEG"
}{
"desc":"FileSize",
"val" : "84227"
},
{
"desc":"StreamSize",
"val" : "0"
},
{
"desc":"File_Modified_Date",
"val" : "UTC 2019-07-16 05:36:32"
},
{
"desc":"File_Modified_Date_Local",
"val" : "2019-07-16 11:06:32"
},
{
"desc":"Width",
"val" : "612"
},
{
"desc":"Height",
"val" : "612"
},
{
"desc":"ColorSpace",
"val" : "YUV"
},
{
"desc":"ChromaSubsampling",
"val" : "4:4:4"
},
{
"desc":"BitDepth",
"val" : "8"
},
{
"desc":"Compression_Mode",
"val" : "Lossy"
},
{
"desc":"StreamSize",
"val" : "1"
}
]

如果不使用 for 循环多次循环,有什么办法可以实现吗?我还需要删除那些不需要的和重复的键。

【问题讨论】:

    标签: python json python-3.x list mediainfo


    【解决方案1】:

    试试:

    dct = {
        "media": {
            "@ref": "/home/mediaworker/divergent.jpg",
            "track": [
                {
                    "@type": "General",
                    "ImageCount": "1",
                    "FileExtension": "jpg",
                    "Format": "JPEG",
                    "FileSize": "84227",
                    "StreamSize": "0",
                    "File_Modified_Date": "UTC 2019-07-16 05:36:32",
                    "File_Modified_Date_Local": "2019-07-16 11:06:32",
                },
                {
                    "@type": "Image",
                    "Format": "JPEG",
                    "Width": "612",
                    "Height": "612",
                    "ColorSpace": "YUV",
                    "ChromaSubsampling": "4:4:4",
                    "BitDepth": "8",
                    "Compression_Mode": "Lossy",
                    "StreamSize": "84227",
                },
            ],
        }
    }
    
    out = []
    for d in dct["media"]["track"]:
        for k, v in d.items():
            if not k.startswith("@"):
                out.append({"desc": k, "val": v})
    
    print(out)
    

    打印:

    [
        {"desc": "ImageCount", "val": "1"},
        {"desc": "FileExtension", "val": "jpg"},
        {"desc": "Format", "val": "JPEG"},
        {"desc": "FileSize", "val": "84227"},
        {"desc": "StreamSize", "val": "0"},
        {"desc": "File_Modified_Date", "val": "UTC 2019-07-16 05:36:32"},
        {"desc": "File_Modified_Date_Local", "val": "2019-07-16 11:06:32"},
        {"desc": "Format", "val": "JPEG"},
        {"desc": "Width", "val": "612"},
        {"desc": "Height", "val": "612"},
        {"desc": "ColorSpace", "val": "YUV"},
        {"desc": "ChromaSubsampling", "val": "4:4:4"},
        {"desc": "BitDepth", "val": "8"},
        {"desc": "Compression_Mode", "val": "Lossy"},
        {"desc": "StreamSize", "val": "84227"},
    ]
    

    【讨论】:

      猜你喜欢
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      相关资源
      最近更新 更多