从CSV创建嵌套JSON

che*_*run 3 python csv json converter

我已经从flat csv中读过Create nested JSON,但在我的情况下它并没有帮助.

我有一个很大的电子表格,使用包含11行和74列的Google Docs创建(某些列未被占用).

我在Google云端硬盘上创建了一个示例.导出时,CSV它看起来像这样:

id,name,email,phone,picture01,picture02,picture03,status
1,Alice,alice@gmail.com,2131232,"image01_01
[this is an image]",image01_02,image01_03,single
2,Bob,bob@gmail.com,2854839,image02_01,"image02_02
[description to image 2]",,married
3,Frank,frank@gmail.com,987987,image03_01,image03_02,,single
4,Shawn,shawn@gmail.com,,image04_01,,,single
Run Code Online (Sandbox Code Playgroud)

现在我想有一个JSON结构,看起来像这样:

{
    "persons": [
        {
            "type": "config.profile",
            "id": "1",
            "email": "alice@gmail.com",
            "pictureId": "p01",
            "statusId": "s01"
        },
        {
            "type": "config.pictures",
            "id": "p01",
            "album": [
                {
                    "image": "image01_01",
                    "description": "this is an image"
                },
                {
                    "image": "image_01_02",
                    "description": ""
                },
                {
                    "image": "image_01_03",
                    "description": ""
                }
            ]
        },
        {
            "type": "config.status",
            "id": "s01",
            "status": "single"
        },
        {
            "type": "config.profile",
            "id": "2",
            "email": "bob@gmail.com",
            "pictureId": "p02",
            "statusId": "s02"
        },
        {
            "type": "config.pictures",
            "id": "p02",
            "album": [
                {
                    "image": "image02_01",
                    "description": ""
                },
                {
                    "image": "image_02_02",
                    "description": "description to image 2"
                }
            ]
        },
        {
            "type": "config.status",
            "id": "s02",
            "status": "married"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

等等其他线路.

我的理论方法是遍历CSV每行的文件(这里开始第一个问题:现在每行等于一行,但有时几行,因此我需要计算逗号?).每行等于一个块config.profile中,包括id,email,pictureId,和statusId(正在生成后两者取决于行编号).

然后,对于每一行,config.pictures生成与id插入config.profile块中的块相同的块.这album是一个与给出图片一样多的元素的数组.

最后,每一行都有一个config.status块,它同样具有与id给定的块相同的块,config.profile以及status具有相应状态的一个条目.

我完全不知道如何创建嵌套和条件JSON文件.

我刚刚得到的地方,我转换的点CSV为有效JSON,没有任何嵌套和附加信息,不直接给定CSV,如type,pictureId,statusId,等.

任何帮助表示赞赏.如果用其他脚本语言(比如ruby)更容易编程,我很乐意切换到那些.

在有人认为这是家庭作业或诸如此类的东西之前.它不是.我只是想自动化一个非常烦人的复制和粘贴任务.

ajd*_*ajd 8

csv模块将很好地处理CSV读取 - 包括处理引号内的换行符.

import csv
with open('my_csv.csv') as csv_file:
   for row in csv.reader(csv_file):
       # do work
Run Code Online (Sandbox Code Playgroud)

csv.reader对象是一个迭代器 - 您可以使用循环遍历CSV中的行for.每行都是一个列表,因此您可以将每个字段作为row[0],row[1]等等.请注意,这将加载第一行(在您的情况下只包含字段名称).

正如我们在第一行中给予我们的字段名,我们可以使用csv.DictReader使每一行的字段可以作为被访问row['id'],row['name']等等.这也将跳过第一行我们:

import csv
with open('my_csv.csv') as csv_file:
   for row in csv.DictReader(csv_file):
       # do work
Run Code Online (Sandbox Code Playgroud)

对于JSON导出,请使用该json模块.json.dumps()将采用Python数据结构,如列表和字典,并返回适当的JSON字符串:

import json
my_data = {'id': 123, 'name': 'Test User', 'emails': ['test@example.com', 'test@hotmail.com']}
my_data_json = json.dumps(my_data)
Run Code Online (Sandbox Code Playgroud)

如果要完全按照发布的方式生成JSON输出,则可以执行以下操作:

output = {'persons': []}
with open('my_csv.csv') as csv_file:
    for person in csv.DictReader(csv_file):
        output['persons'].append({
            'type': 'config.profile',
            'id': person['id'],
            # ...add other fields (email etc) here...
        })

        # ...do similar for config.pictures, config.status, etc...

output_json = json.dumps(output)
Run Code Online (Sandbox Code Playgroud)

output_json 将包含您想要的JSON输出.

但是,我建议你仔细考虑你所追求的JSON输出的结构 - 目前,你正在定义一个没有用处的外部字典,而你正在config直接添加所有' '数据' persons' - 你可能想重新考虑这一点.