python - How to break a large CSV data file into individual data files? -
i have csv file first row of contains variables names , rest of rows contains data. what's way break files each containing 1 variable in python? solution going robust? e.g. if input file 100g in size? trying perform divide conquer strategy new python. in advance help!
the input files looks like
var1,var2,var3 1,2,hello 2,5,yay ...
i want create 3 (or many variables) files var1.csv, var2.csv, var3.csv files resemble file1
var1 1 2 ...
file2
var2 2 5 ...
file3
var3 hello yay
as lomg number of columns isn't absurdly huge (larger number of files can have open @ once on platform), number of rows, , total size, no big deal (as long of course have ample free space on disk;-) since you'll processing column @ time -- suggest following code:
import csv def splitit(inputfilename): open(inputfilename, 'rb') inf: inrd = csv.reader(inf) names = next(inrd) outfiles = [open(n+'.csv', 'wb') n in names] ouwr = [csv.writer(w) w in outfiles] w, n in zip(ouwr, names): w.writerow([n]) row in inrd: w, r in zip(ouwr, row): ouwr.writerow([r]) o in outfiles: o.close()
Comments
Post a Comment