Reputation: 3088
I am trying to extract a word from a first line of file:
LOCATION,Feij�,AC,a,b,c
this way:
2.0.0-p247 :005 > File.foreach(file).first
=> "LOCATION,Feij\xF3,AC,a,b,c\r\n"`
but when I try to use split:
2.0.0-p247 :008 > File.foreach(file).first.split(",")
ArgumentError: invalid byte sequence in UTF-8 from (irb):8:in
split' from (irb):8 from /home/bleh/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in
'
What I expected is: Feijó
I already try a lot of combinations like .encode and .force_encoding.
Some ideas?
Upvotes: 0
Views: 1603
Reputation: 79813
The character ó
is \xF3
in the ISO-8859-1 encoding, so this is probably the encoding of the file (it could also be CP-1252.
You can specify the encoding as an arg to File::foreach
, and you can also ask Ruby to re-encode it to UTF-8 for you:
File.foreach(file, :encoding => 'iso-8859-1:utf-8').first.split(",")
Upvotes: 3