Splitting one csv into multiple files

In Python

Use readlines() and writelines() to do that, here is an example:

>>> csvfile = open('import_1458922827.csv', 'r').readlines()
>>> filename = 1
>>> for i in range(len(csvfile)):
...     if i % 1000 == 0:
...         open(str(filename) + '.csv', 'w+').writelines(csvfile[i:i+1000])
...         filename += 1

the output file names will be numbered 1.csv, 2.csv, … etc.

From terminal

FYI, you can do this from the command line using split as follows:

$ split -l 1000 import_1458922827.csv

Leave a Comment

tech