codec
codec

Reputation: 8806

Reading a CSV file with headers having double quotes

I have .csv file with the header row as

VM,Mac Address,Type,"IP Address"

With the following code I can print the value of VM but not IP Address. If remove the "" surrounding IP Address it works.

CSV.foreach(vcenter_vnetwork_file, headers: true, :quote_char => "\x00") do |row|
    puts row['VM']
    puts row['IP Address']
end

I tried using puts row['\"IP Address\"'] but this did not work. I can programmatically remove those "" from the header row but is there any way to get this working woring without any workaround? I also tried adding :force_quotes => true in the CSV.foreach loop.

Upvotes: 0

Views: 2144

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

Why would you set the :quote_char to be "\x00"? Set it to a quote indeed.

CSV.foreach('/tmp/qqq.csv', headers: true, :quote_char => '"') do |row|
  puts row['IP Address']
end

Upvotes: 3

Related Questions