使用python将csv文件转换为字典的嵌套嵌套列表

时间:2020-09-10 15:13:47

标签: python json csv

我正在尝试将csv文件转换为嵌套的json:我正在寻找类似这样的json输出。

这是我的json数据:

[
   {
      "RID":"test",
      "UId":"userl@gmail.com",
      "Ugroup":"abc",
      "Query":[
         {
            "Status":"query1",
            "ECategory":"error1"
         },
         {
            "Status":"query2",
            "ECategory":"error2"
         }
      ],
      "SuccesfulQ":"7",
      "FailedQ":"0"
   },
   {
      "RID":"test2",
      "Uld":"user2@gmail.com",
      "Ugroup":"xyz",
      "Query":[
         {
            "Statement":"query3",
            "ECategory":"error3"
         },
         {
            "Statement":"query4",
            "ECategory":"error4"
         }
      ],
      "SuccesfulQ":"7",
      "FailedQ":"0"
   }
]

这是我的CSV文件

[csvfile] [2]

    RID,UId,Ugroup,Query.Statement,Query.ECategory,SuccesfulQ,FailedQ
   test,user1@gmail.com,abc,query1,error1,            7,        0
   test,user1@gmail.com,abc,query2,error2             7,        0
   test2,user2@gmail.com,xyz,query3,error3            7,        0
   test2,user2@gmail.com,xyz,query4,error4            7,        0

使用下面的代码,我得到的不是嵌套的普通json:

import csv
import json
def make_json(csvFilePath):
    data = {}
    with open(csvFilePath, encoding='utf-8') as csvf:
        a = [{k:v for k,v in row.items()} for row in csv.DictReader(csvf, skipinitialspace=True)]
        print(a)
csvFilePath = r'table.csv'
make_json(csvFilePath)

1 个答案:

答案 0 :(得分:0)

在这种情况下,创建字典列表后,您可以遍历奇数行并追加偶数行中的数据。之后,您可以使用json.dumps将完整列表转换为json。

尝试此代码。假设每个用户查询有2行:

cs = '''
RID,UId,Ugroup,Statement,ECategory,SuccesfulQ,FailedQ
test1,user1@gmail.com,abc,query1,error1,7,0
test1,user1@gmail.com,abc,query2,error2,7,0
test2,user2@gmail.com,xyz,query3,error3,7,0
test2,user2@gmail.com,xyz,query4,error4,7,0
'''.strip()

with open('table.csv','w') as f: f.write(cs)  # write test file

############# Main Script ##############

import csv
import json

def make_json(csvFilePath):
    data = {}
    lst = []    
    with open(csvFilePath, encoding='utf-8') as csvf:
        a = [{k:v for k,v in row.items()} for row in csv.DictReader(csvf, skipinitialspace=True)]
        for i in range(0,len(a),2):  # odd rows
           a[i]['Query'] = [  # combined data, new column
           {'Statement':a[i]['Statement'],'ECategory':a[i]['ECategory']},
           {'Statement':a[i+1]['Statement'],'ECategory':a[i+1]['ECategory']}
           ]           
           del a[i]['Statement'] # remove old columns
           del a[i]['ECategory']
           lst.append(a[i])  # final list  # odd rows only
        return json.dumps(lst)
        
csvFilePath = r'table.csv'
jsnFilePath = r'table.json'
jsn = make_json(csvFilePath)

with open (jsnFilePath,'w') as f:
   f.write(jsn)

输出

[
    {
        "RID": "test1",
        "UId": "user1@gmail.com",
        "Ugroup": "abc",
        "SuccesfulQ": "7",
        "FailedQ": "0",
        "Query": [
            {
                "Statement": "query1",
                "ECategory": "error1"
            },
            {
                "Statement": "query2",
                "ECategory": "error2"
            }
        ]
    },
    {
        "RID": "test2",
        "UId": "user2@gmail.com",
        "Ugroup": "xyz",
        "SuccesfulQ": "7",
        "FailedQ": "0",
        "Query": [
            {
                "Statement": "query3",
                "ECategory": "error3"
            },
            {
                "Statement": "query4",
                "ECategory": "error4"
            }
        ]
    }
]