list - chunking a large csv file by offset [0] in python -
i have csv file in format:
1,data,data2,data3..... 1,data,data2,data3..... 2,data,data2,data3..... 2,data,data2,data3..... 3,data,data2,data3..... 3,data,data2,data3.....
i need chunk these strings offset[0] list such multiple lists with:
1,data,data2,data3..... 1,data,data2,data3.....
and another:
2,data,data2,data3..... 2,data,data2,data3.....
etc. have following code:
import csv filename = 'somefile.csv' open(filename) csv_file: readcsv = csv.reader(csv_file, delimiter=',') chunk1 = [] row in csv_file: if row[0] '1': print(row) else: break
this gives first chunk need adapt chunks of same of set in lists.
assuming they're sorted first column, it:
import csv itertools import groupby operator import itemgetter filename = 'somefile.csv' open(filename) csv_file: csvreader = csv.reader(csv_file, delimiter=',') chunks = [list(g) k, g in groupby(csvreader, itemgetter(0))]
Comments
Post a Comment