Search code examples
sqlrubycsvfastercsv

Ruby CSV read strings and numbers from MySQL exported csv


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


Solution

  • Using the input you provided, I am unable to reproduce this.

    1.9.3p194 :001 > require 'csv'
     => true 
    1.9.3p194 :002 > CSV.foreach("test.txt", {:headers => true}) { |row| puts row }
    1,Bob,[email protected],omnomnom
    2,Charlie,[email protected],andcheese
    4,Doug,[email protected],usemeltattack
     => nil
    

    The only difference I see between our environments is that you are using rbenv, and I am using RVM. I also verified this on another machine I have with ruby 1.9.3-p194. Does the input you provided exactly match what is in your csv?