0
votes

I am trying to read a CSV file in Ruby 1.9.3 (I am not using Rails.)

sessions = CSV.read("c:/scripts/ruby/testcsvencoding.csv", :headers => true, 
:encoding => "UTF-8") 

sessions.each do | session |
  p session['col1']    <-- does not work
  p session[0]   <--- works
end

The file contains:

col1, col2
a,1
b,2

I saw what seems like "Avoding “Invalid byte sequence in UTF-8″ with Ruby and CSV files", but it may not be the same problem as mine.

When I try the workaround there I get an error.

Is there any way to solve this? Is this a known problem?

This is on Windows

1
This works just fine on my OS X machine, maybe this is a bug in Ruby on Windows? What is the exact error?rdvdijk
did you save the csv file as utf8nilanjan

1 Answers

0
votes

That error means there's a bad utf-8 byte sequence in your data. If that bothers you, fix the data. Otherwise try ascii-8bit.