Last active
December 21, 2018 06:36
-
-
Save shockby/49881fc352f8d806068d6d8d75a74caf to your computer and use it in GitHub Desktop.
CSVファイルを分割します。分割したファイルにヘッダーを用意します。
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
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