Created
July 11, 2017 15:57
-
-
Save lucindo/2e3d342eaec9acafb742e5adb6d24231 to your computer and use it in GitHub Desktop.
Splits CSV files by line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import sys | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
sys.exit("%s <csv-file> <lines>" % sys.argv[0]) | |
infile, total = sys.argv[1], int(sys.argv[2]) | |
basename = infile.split('.csv')[0] | |
output_count = 0 | |
output_handler, file_handler = None, None | |
line_count = 0 | |
with open(infile, 'rU') as csvinput: | |
reader = csv.reader(csvinput) | |
header = reader.next() | |
for row in reader: | |
if line_count >= total: | |
file_handler.close() | |
output_handler, file_handler = None, None | |
line_count = 0 | |
if output_handler is None: | |
output_count += 1 | |
filename = "%s_%03d.csv" % (basename, output_count) | |
file_handler = open(filename, 'w') | |
print "writing file:", filename | |
output_handler = csv.writer(file_handler) | |
output_handler.writerow(header) | |
output_handler.writerow(row) | |
line_count += 1 | |
if output_handler is not None: | |
file_handler.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment