I exported tables and queries from SQL.
The ruby (1.9+) way to read csv appears to be:
require 'csv'
CSV.foreach("exported_mysql_table.csv", {:headers=>true}) do |row|
puts row
end
Which works great if your data is like this:
"name","email","potato"
"Bob","[email protected]","omnomnom"
"Charlie","[email protected]","andcheese"
"Doug","[email protected]","usemeltattack"
Works fine (The first line is a header, the attributes). However, if the data is like this:
"id","name","email","potato"
1,"Bob","[email protected]","omnomnom"
2,"Charlie","[email protected]","andcheese"
4,"Doug","[email protected]","usemeltattack"
Then we get the error:
.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/csv.rb:1894:in `block (2 levels) in shift': Missing or stray quote in line 2 (CSV::MalformedCSVError)
I think this is because the id is stored as a number, not a string, and thus has no quotes, and the csv parser expects ALL the entries to have quotes. Ideally I'd like to read "Bob" as a string and 1 as a number (and stuff it into a Hash of hashes)
(Have tried 'FasterCSV', that gem became 'csv' since ruby 1.9)
EDIT:
Was pointed out that the example worked fine (derp), was looking in the wrong place, it was an error with multi-line fields, question moved to Ruby CSV read multiline fields