Created
March 22, 2018 23:28
-
-
Save ryantuck/d0cc9e1be3c9a79752ba04b7330cb2af to your computer and use it in GitHub Desktop.
splitting csvs in python
This file contains hidden or 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 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