0

I am trying to write a UTF-8 character in a CSV file with csv library of Ruby. And I have got an error:

csv ruby write problem ASCII-8BIT (Encoding::CompatibilityError)

#create csv file
CSV.open(CSV_file,"wb",) do |csv|
  csv << First_line
  rows.each do  |r|
    csv << r.generate_array
  end
end

That's the code where UTF-8 conflicts with ASCII-8BIT.

Example text that fails:

demás

6
  • ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux-gnu] Ubuntu 16.04 uname -r 4.10.0-28-generic Commented Jul 30, 2017 at 19:09
  • I want to write UTF-8 into a the file. Commented Jul 30, 2017 at 19:10
  • r is a class called CSV_row that I have create. They are objects generated in the same script. Taking data from others files, and generating a object with text information that will be inserted to the CSV. Commented Jul 30, 2017 at 19:16
  • If you write utf-8, why not use CSV.open(CSV_file,"w:utf-8"? YOur example can't be used to show details. E.g. what is rows in your code? Commented Jul 30, 2017 at 19:35
  • Please add example text Commented Jul 30, 2017 at 19:55

1 Answer 1

1

Here is an example of CSV writing and reading with UTF-8:

fn="/tmp/f.csv"

require "csv"
d1=DATA.read.split(/\n/).map {|e| e.split}
CSV.open(fn, "w:utf-8") do |row|
   d1.each { |dr| row << dr }
end

d2=[]
CSV.foreach(fn) do |row|
  d2 << row
end

puts d1==d2
# true

__END__
privé face à face à un tête-à-tête
Face to face with one-on-one
demás

Without a more detailed example from you, I cannot help further.

Sign up to request clarification or add additional context in comments.

3 Comments

the problem was that I read before a file, and I didn't force the read enconde to utf-8. So in the objects I had the information in other encoding. And it was the problem. When you need to write uft-8 ensure to read. File.open(path,'r:utf-8'){|file| file.read} Thanks!
If you do not specify utf-8 when you open a file for writing, its encoding will depend on the encoding of the source strings written. It may be that you are mixing strings with different types of encodings into a single array?
Yes, I had strings with differents encodings. Because one of the attributes of the objects was a attribute that store information read from a file. When I read that file, I did not specify the enconding reading. So when I specify read like utf-8. Every string of the object had the same encoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.