I have a csv
file - file1.csv
, which has 3 columns in each row. The sample looks like this:
A,d1,200
A,d2,250
A,d3,10
B,d1,100
B,d2,150
B,d4,45
.
.
.
The structure of above data is - loacation_id,dept_id,num_emp
.Now what I want to do is break the records of the csv file into chunks based on the 1st column value so that in one chunk there are records for only location, and then pass these chunks to a function one by one. I wrote this code based on this SO post but I am getting error as TypeError: 'itertools._grouper' object has no attribute '__getitem__'
. My current code is:
import csv
from itertools import groupby
def func(chunk):
for line in chunk:
print line
file_read = open('file1.csv', 'r')
reader = csv.reader(file_read)
for rows in groupby(reader):
func(rows)
How can I break the records into chunks based on values in one column and pass the chunks to a function?