我想将csv转换为多级json。我想要的是来自:

brand,  active, prod   , prod_id, part, part_id, part_prise
canon,  1     , camera , 132    , body,  543   , 399
canon,  0     , camera , 132    , lens,  543   , 399
canon,  1     , printer, 132    , cart,  543   , 399
canon,  1     , printer, 872    , ink ,  543   , 399
sony ,  1     , camera , 132    , body,  543   , 399
sony ,  1     , lcd    , 112    , body,  543   , 399
sony ,  0     , lcd    , 132    , body,  543   , 399
sony ,  1     , laptop , 162    , body,  543   , 399


以下应该是其JSON文件。

brand : "canon",
active : 1,
prod : [{
     prod : "camera",
     prod_id : 132,
     part : [{
            part : "body",
            part_id : 543,
            part_prise:399
            },
           {
            part : "lens",
            part_id : 543,
            part_prise:399
            }]
         },{
     prod : "printer",
     prod_id : 132,
     part : [{
            part : "cart",
            part_id : 543,
            part_prise:399
            },
           {
            part : "ink",
            part_id : 543,
            part_prise:399
            }]
         }
      ]
...
...
...and so on..

最佳答案

使用csv dictreader,它在python csv包中很流行

09-27 13:07