Skip to content

Instantly share code, notes, and snippets.

@ryantuck
Created March 22, 2018 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryantuck/d0cc9e1be3c9a79752ba04b7330cb2af to your computer and use it in GitHub Desktop.
Save ryantuck/d0cc9e1be3c9a79752ba04b7330cb2af to your computer and use it in GitHub Desktop.
splitting csvs in python
import csv
import os
source_filename = 'all.csv'
records_per_file = 1000
target_file_prefix = 'target/split'
with open(source_filename, 'r') as source:
reader = csv.reader(source)
headers = next(reader)
file_idx = 0
records_exist = True
while records_exist:
i = 0
target_filename = f'{target_file_prefix}_{file_idx}.csv'
with open(target_filename, 'w') as target:
writer = csv.writer(target)
while i < records_per_file:
if i == 0:
writer.writerow(headers)
try:
writer.writerow(next(reader))
i += 1
except:
records_exist = False
break
if i == 0:
# we only wrote the header, so delete that file
os.remove(target_filename)
file_idx += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment