I'm trying to insert new array inside the array but I'm not sure where can I append the data.
Csv table
date, id, description, name, code
2016-07-01, S56202, Class A, Jacky, 300-E003
Currently, my result is
Here is my code
I need to insert another array in
Expected result
Csv table
date, id, description, name, code
2016-07-01, S56202, Class A, Jacky, 300-E003
Currently, my result is
1 2 3 4 5 |
"date" : "2016-07-01" , "id" : "S56202" , "items" : [{ "description" : "Class A" , "code" : "300-E003" , "name" : "Jacky" }, |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import csv import json from itertools import groupby with open ( 'student.csv' , 'r' ) as csv_ledger: r = csv.DictReader(csv_ledger) data = [ dict (d) for d in r] groups = [] for k, g in groupby(data, lambda r: (r[ 'id' ], r[ 'date' ])): groups.append({ "date" : k[ 1 ], "id" : k[ 0 ], "items" : [{k: v for k, v in d.items() if k not in [ 'ref_num' , 'date' ]} for d in list (g)]}) |
items
then add the email
, code
and name
into the new array.Expected result
1 2 3 4 |
"items" : [{ "description" : "Class A" , "new_account" : { "email" : "abc@hotmail.com" , "code" : "300-E003" , "name" : "Jacky" }}] |