I have CSV file given below
CODE,PRODUCT
101,item_1
101,item_1
101,item_2
102,item_3
103,item_2
104,item_5
I need to convert into dictionary as given below
{101:{'item_1':2, 'item_2':1},
102:{'item_3':1},
103:{'item_2':1},
104:{'item_5':1}
I have read file using CSV
import csv
def csv_reader(file_obj):
reader = csv.reader(file_obj)
for row in reader:
print(" ".join(row))
if __name__ == "__main__":
csv_path = 'g.csv'
with open(csv_path, "r") as f_obj:
csv_reader(f_obj)
How to convert to dictionary
{101:{'item_1':2, 'item_2':1},
102:{'item_3':1},
103:{'item_2':1},
104:{'item_5':1}