Reputation: 15117
There are soo many inherent problems with csv, 1) your columns can't have commas, so you have to encapsulate them with quotes "", and then once you encapsulate them with quotes, you have to already escape the quotes within a sentence by using \"
What is the easiest way to parse a csv file? I reverted to doing semi-colon separated files but those are troublesome when working in excel, so now i'm back to csv files.
Upvotes: 1
Views: 861
Reputation: 2929
Here is a rough example using CSV in ruby:
class DataLoader
require 'csv'
def self.import_csv
Dir.glob("/imports/*.csv").each do |csv_file|
csv = CSV.open(csv_file, {:col_sep => ",", :headers => true, :return_headers => false, :quote_char => '"'})
@data_table = csv.read
@data_table.each do |data_row|
field_one = data_row.first[0]
field_two = data_row.first[1]
#do some work
end
end
end
Upvotes: 1
Reputation: 10349
Check out Faster CSV from James Edward Gray II.
"FasterCSV is CSV, but faster, smaller, and cleaner."
Upvotes: 2
Reputation: 1406
Have you tried http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html.
You can also look at http://fastercsv.rubyforge.org/
Upvotes: 4