0

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","bob@bob.bob","omnomnom"
"Charlie","char@char.com","andcheese"
"Doug","diggyd@diglet.com","usemeltattack"

Works fine (The first line is a header, the attributes). However, if the data is like this:

"id","name","email","potato"
1,"Bob","bob@bob.bob","omnomnom"
2,"Charlie","char@char.com","andcheese"
4,"Doug","diggyd@diglet.com","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

Community
  • 1
  • 1
xxjjnn
  • 14,591
  • 19
  • 61
  • 94

1 Answers1

1

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,bob@bob.bob,omnomnom
2,Charlie,char@char.com,andcheese
4,Doug,diggyd@diglet.com,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?

Eugene
  • 4,829
  • 1
  • 24
  • 49
  • Ahhhh the problem is with multi-line! I should have given a better example, (and actually tested this one -_-). Going to start NEW question. Thanks! – xxjjnn Oct 16 '12 at 12:55