Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
require 'csv'
# input: last_name;first_name;kto;blz;amount
# output: last_name;first_name last_name;kto;blz;amount
CSV.open "output.csv", "wb", :col_sep => ';' do |output_row|
CSV.foreach "input.csv", :col_sep => ';' do |input_row|
name, firstname, kto, blz, amount = *input_row
output_row << [name, "#{firstname} #{name}", kto, blz, amount]
end
end
# 36 secs on my MBP for 1 mio entries
require 'csv'
require 'faker'
# last_name;first_name;kto;blz;amount
CSV.open "input.csv", "wb", :col_sep => ';' do |row|
1_000_000.times do
row << [Faker::Name.last_name, Faker::Name.first_name, (rand*10**8).to_i, (rand*10**10).to_i, (rand*10**4).to_i]
end
end
# 85 secs on my MBP for 1 mio entries
# input: last_name;first_name;kto;blz;amount
# output: last_name;first_name last_name;kto;blz;amount
File.open "output.csv", "w+" do |output_file|
File.open "input.csv" do |input_file|
while line = input_file.gets
line.chomp!
name, firstname, kto, blz, amount = *line.split(';')
output_file << [name, "#{firstname} #{name}", kto, blz, amount, "\n"].join(';')
end
end
end
# 7 secs on my MBP for 1 mio entries
Owner

niko commented Jun 5, 2011

csv is in the Ruby standard lib, faker is a gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment