在python中使用Json列表创建CSV文件,列中包含名称以保持有序

wvt8vs2t  于 2023-03-31  发布在  Python
关注(0)|答案(1)|浏览(94)

这个问题看起来很简单,但在python中进行转换很困难。
问题是我有一个json,但我需要在不使用for循环的情况下将其转换为csv。该文件是嵌套的,并且是一个很长的列表,因此必须保持组织。下面是一个所需输出为csv的示例

"output 2.incedent","output 2.prediction_image","output 2.filename","output 2.time","output 2.orignal_filename","output 1.incedent","output 1.prediction_image","output 1.filename","output 1.time","output 1.orignal_filename"
"","null","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:00.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8",,,,,
,,,,,"audio","a woman holding a cell phone in front of a picture of a cat","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:06.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
,,,,,"audio","a cat sitting on top of a computer screen","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:08.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
,,,,,"audio","a woman is looking at a screen on a laptop","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:04.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
,,,,,"audio","a woman holding a cell phone in front of her face","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:00.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
,,,,,"audio","a cat sitting on top of a computer screen","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:02.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
,,,,,"audio","a picture of a woman on a computer screen","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:07.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
,,,,,"audio","a picture of a cat on a computer screen","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:05.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
,,,,,"audio","a cat sitting on top of a computer screen","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:01.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
,,,,,"audio","a woman is looking at a screen on a laptop","/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4","0:00:03.00"," https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
"OAAS O  A NONI ONIIEIN NO ONWIAE MOION ROM I OER OLE ST","null","/downloads/2ad14364e0594a47a39633fa90053b77.mp4","0:00:00.00"," https://www.xyztv.com/share/2ad14364e0594a47a39633fa90053b77",,,,,
,,,,,"audio","a computer screen with a cartoon character on it","/downloads/2ad14364e0594a47a39633fa90053b77.mp4","0:00:18.00"," https://www.xyztv.com/share/2ad14364e0594a47a39633fa90053b77"

上面所需的输出应该由类似于下面的内容创建

[
    {
        "output 2": [
            {
                "dialog": "",
                "incedent": "null",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:00.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            }
        ],
        "output 1": [
            {
                "dialog": "audio",
                "incedent": "a woman holding a cell phone in front of a picture of a cat",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:06.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            },
            {
                "dialog": "audio",
                "incedent": "a cat sitting on top of a computer screen",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:08.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            },
            {
                "dialog": "audio",
                "incedent": "a woman is looking at a screen on a laptop",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:04.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            },
            {
                "dialog": "audio",
                "incedent": "a woman holding a cell phone in front of her face",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:00.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            },
            {
                "dialog": "audio",
                "incedent": "a cat sitting on top of a computer screen",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:02.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            },
            {
                "dialog": "audio",
                "incedent": "a picture of a woman on a computer screen",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:07.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            },
            {
                "dialog": "audio",
                "incedent": "a picture of a cat on a computer screen",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:05.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            },
            {
                "dialog": "audio",
                "incedent": "a cat sitting on top of a computer screen",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:01.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            },
            {
                "dialog": "audio",
                "incedent": "a woman is looking at a screen on a laptop",
                "filename": "/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4",
                "time": "0:00:03.00",
                "orignal_filename": " https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8"
            }
        ]
    },
    {
        "output 2": [
            {
                "dialog": "OAAS O  A NONI ONIIEIN NO ONWIAE MOION ROM I OER OLE ST",
                "incedent": "null",
                "filename": "/downloads/2ad14364e0594a47a39633fa90053b77.mp4",
                "time": "0:00:00.00",
                "orignal_filename": " https://www.xyztv.com/share/2ad14364e0594a47a39633fa90053b77"
            }
        ],
        "output 1": [
            {
                "dialog": "audio",
                "incedent": "a computer screen with a cartoon character on it",
                "filename": "/downloads/2ad14364e0594a47a39633fa90053b77.mp4",
                "time": "0:00:18.00",
                "orignal_filename": " https://www.xyztv.com/share/2ad14364e0594a47a39633fa90053b77"
            
            }
        ]
    }
]

以下是我目前拥有的

import pandas as pd

import json

with open('data.json') as data_file:    
    data = json.load(data_file)  

df = pd.json_normalize(data, 'output 2', ['output 1'], 
                    record_prefix='output 2_')

print(df)
ni65a41a

ni65a41a1#

我将使用concat

df = (
        pd.read_json("data.json")
        .pipe(lambda df_: pd.concat([pd.json_normalize(
                df_[k].explode()).add_prefix(f"{k}.") 
                for k in df_.columns], axis=0))
    )

输出(.csv):

output 2.dialog,output 2.incedent,output 2.filename,output 2.time,output 2.orignal_filename,output 1.dialog,output 1.incedent,output 1.filename,output 1.time,output 1.orignal_filename
,null,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:00.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8,,,,,
OAAS O  A NONI ONIIEIN NO ONWIAE MOION ROM I OER OLE ST,null,/downloads/2ad14364e0594a47a39633fa90053b77.mp4,0:00:00.00, https://www.xyztv.com/share/2ad14364e0594a47a39633fa90053b77,,,,,
,,,,,audio,a woman holding a cell phone in front of a picture of a cat,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:06.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8
,,,,,audio,a cat sitting on top of a computer screen,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:08.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8
,,,,,audio,a woman is looking at a screen on a laptop,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:04.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8
,,,,,audio,a woman holding a cell phone in front of her face,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:00.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8
,,,,,audio,a cat sitting on top of a computer screen,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:02.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8
,,,,,audio,a picture of a woman on a computer screen,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:07.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8
,,,,,audio,a picture of a cat on a computer screen,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:05.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8
,,,,,audio,a cat sitting on top of a computer screen,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:01.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8
,,,,,audio,a woman is looking at a screen on a laptop,/downloads/0d28e8927e9c4750b4a281e5814f8ac8.mp4,0:00:03.00, https://www.xyztv.com/share/0d28e8927e9c4750b4a281e5814f8ac8
,,,,,audio,a computer screen with a cartoon character on it,/downloads/2ad14364e0594a47a39633fa90053b77.mp4,0:00:18.00, https://www.xyztv.com/share/2ad14364e0594a47a39633fa90053b77

相关问题