user2156331
user2156331

Reputation: 11

I can't get headers when I parse csv file with Ruby

I can't get headers. No error happens. How can I get it properly?

Data:

"A","B","C"
"1","2","3"

Ruby:

require "csv"

table = CSV.read("filename", :headers => true)

puts table[0] # "1","2","3"

puts table[headers] # Nothing happens.

Upvotes: 1

Views: 114

Answers (1)

Blender
Blender

Reputation: 298532

Try table.headers:

irb(main):006:0> table.headers
=> ["A", "B", "C"]

How are you running this code? table[headers] should return an error:

irb(main):008:0> table[headers]
NameError: undefined local variable or method `headers' for main:Object
    from (irb):8
    from /usr/bin/irb:12:in `<main>'

Upvotes: 2

Related Questions