Created
November 27, 2018 20:35
-
-
Save bassemfg/ad0d79521e087a08b3dd4155d98e9ee5 to your computer and use it in GitHub Desktop.
split_csv_by_row_limit
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Nov 27 12:06:43 2018 | |
@author: Bassem Yacoube | |
""" | |
import csv | |
import sys | |
import os | |
print ("Please delete the previous created files. If any.") | |
filepath = 'uF:\OneDrive - EY\data' # input("Enter the File path: ") | |
line_count = 0 | |
filenum = 1 | |
try: | |
in_file = input("Enter Input File name: ") | |
if in_file[-4:] == ".csv": | |
split_size = 1048570 # int(input("Enter size: ")) | |
print ("Split Size ---", split_size) | |
print (in_file, " will split into", split_size, "rows per file named as OutPut-file_*.csv (* = 1,2,3 and so on)") | |
with open (in_file,'r') as file1: | |
row_count = 0 | |
#reader = csv.reader(file1) # unused here | |
out_file = open(filepath + "\\OutPut-file_" +str(filenum) + ".csv", "a") | |
for line in file1: | |
#print line | |
if row_count >= split_size: | |
out_file.close() | |
filenum = filenum + 1 | |
out_file = open(filepath + "\\OutPut-file_" +str(filenum) + ".csv", "a") | |
row_count = 0 | |
out_file.write(line) | |
row_count = row_count +1 | |
line_count = line_count+1 | |
print ("Total rows Written --", row_count) | |
else: | |
print ("Please enter the Name of the file correctly") | |
except IOError as e: | |
print ("Oops..! Please Enter correct file path values", e) | |
except ValueError: | |
print ("Oops..! Please Enter correct values") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment