Skip to content

Instantly share code, notes, and snippets.

@barunthapa
Last active December 20, 2015 01:59
Show Gist options
  • Save barunthapa/6052723 to your computer and use it in GitHub Desktop.
Save barunthapa/6052723 to your computer and use it in GitHub Desktop.
CSV Splitter
require 'csv'
file_naming_count = 0
row_count = 0
# basic file and directory naming
parent_csv_filename = "/folder/filename.csv"
csv_output_directory = "/folder/splitted/"
f=File.open(parent_csv_filename)
#set the number into which the parent csv is to be sliced into
number_of_row_in_each_file = 10000
adjuster = f.readlines.size/number_of_row_in_each_file
adjuster = adjuster.to_s.size
base_file_name = "base_file_name"
formatted_file_naming_count = file_naming_count.to_s.rjust(adjuster, '0')
incremental_file_name = "#{base_file_name}_#{formatted_file_naming_count}"
HEADERS = CSV.open(parent_csv_filename, "r").first
flag = false
CSV.open(parent_csv_filename, "r") do |csvs|
csvs.each do |csv|
if flag
if row_count >= number_of_row_in_each_file
file_naming_count += 1
formatted_file_naming_count = file_naming_count.to_s.rjust(adjuster, '0')
incremental_file_name = "#{base_file_name}_#{formatted_file_naming_count}"
row_count = 0
end
CSV.open("#{csv_output_directory}#{incremental_file_name}.csv", "a+") do |write_csv|
if row_count == 0
write_csv << HEADERS
end
row_count += 1
write_csv << csv
end;nil
end;nil
flag = true
print "\r#{file_naming_count}"
end;nil
end;nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment