I have a string with extra whitespace:
First,Last,Email ,Mobile Phone ,Company,Title ,Street,City,State,Zip,Country, Birthday,Gender ,Contact Type
I want to parse this line and remove the whitespaces.
My code looks like:
namespace :db do
task :populate_contacts_csv => :environment do
require 'csv'
csv_text = File.read('file_upload_example.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|
puts "First Name: #{row['First']} \nLast Name: #{row['Last']} \nEmail: #{row['Email']}"
end
end
end
CSV supports "converters" for the headers and fields, which let you get inside the data before it's passed to your
each
loop.Writing a sample CSV file:
Here's how I'd do it:
Which outputs:
The converters simply strip leading and trailing whitespace from each header and each field as they're read from the file.
Also, as a programming design choice, don't read your file into memory using:
Then parse it:
Then loop over it:
Ruby's IO system supports "enumerating" over a file, line by line. Once my code does
CSV.open
the file is readable and theeach
reads each line. The entire file doesn't need to be in memory at once, which isn't scalable (though on new machines it's becoming a lot more reasonable), and, if you test, you'll find that reading a file usingeach
is extremely fast, probably equally fast as reading it, parsing it then iterating over the parsed file.The nil test is added to the row but not header converters assuming that the headers are never nil, while the data might be, and nil doesn't have a strip method. I'm really surprised that, AFAIK, :strip is not a pre-defined converter!
You can
strip
your hash first:Edited to strip hash keys too