Skip to content

Instantly share code, notes, and snippets.

@shockby
Last active December 21, 2018 06:36
Show Gist options
  • Save shockby/49881fc352f8d806068d6d8d75a74caf to your computer and use it in GitHub Desktop.
Save shockby/49881fc352f8d806068d6d8d75a74caf to your computer and use it in GitHub Desktop.
CSVファイルを分割します。分割したファイルにヘッダーを用意します。
filepath = "in.csv"
out_file = "out"
line_count = 250
lines = IO.readlines(filepath)
total_page = (lines.count - 1) / line_count + 1
header = lines.first
(1.. total_page).each do |page|
File.open("#{out_file}#{page}.csv","w") do |text|
start_pos = line_count * (page - 1) + 1
text.puts header
text.puts lines[start_pos, line_count]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment