0

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}
1
  • Look at itertools.groupby and collections.Counter. Commented Jan 21, 2019 at 6:42

3 Answers 3

1
from collections import defaultdict, Counter

parsed = defaultdict(Counter)
reader = csv.reader(file_obj)
code_index = 0
product_index = 1
for row in reader:
    if row and len(row) >= 2:
        code = row[code_index]
        product = row[product_index]
        parsed[code][product] += 1

I think this will do the job.

Sign up to request clarification or add additional context in comments.

4 Comments

got error product = row[product_index IndexError: list index out of range
Probably an issue with the line length.. I've added a check for the expected number of elements, but you should consider the best way to validate your incoming data.
Can we iterate from csv with counter. I am getting 'parsed' output as defaultdict(collections.Counter, {})
Yes, parsed is basically a nested dictionary. The counter Object provides some extra helper methods, but provides standard dictionary functionality.
1

Since, each row is returned as a list of strings from csv.reader, you should try like below:

result_dict = {}
for row in reader:
    if row[0] in result_dict:
        if row[1] in result_dict[row[0]]:
            result_dict[row[0]][row[1]] += 1
        else:
            result_dict[row[0]][row[1]] = 1
    else:
        result_dict[row[0]] = {row[1]:1}

Note: This is just a logic outline. Please fit it into your code appropriately with additional checks as required.

Comments

0

Courtesy : @monkut

import csv
from collections import defaultdict, Counter
parsed = defaultdict(Counter)
file_obj = open(r'C:\Users\maws\Desktop\g.csv', 'r')
reader = csv.reader(file_obj)
code_index = 0
product_index = 1
next(file_obj)
for row in file_obj:
    row = row.split(',')
    #print (row)
    if row and len(row) >= 2:
        code = row[code_index]
        #print(code)
        product = row[product_index]
        parsed[code][product.strip()] += 1
parsed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.