Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write and read a file with utf-8 encoding

Tags:

ruby

utf-8

I've been reading up on all the UTF-8 related questions and blog posts, and I've got the following example in a test.rb file:

# encoding: UTF-8 File.open("test.txt", "w") do |f|   f.write "test © foo" end  File.open("test.txt", "r") do |f|   puts f.read end 

this works perfectly. is produces the © symbol correctly in the file, and it reads the © back to me and prints it on the screen.

but when I use this same code in my actual project, i get this written to the file instead of the © symbol: \u00A9

FWIW: I'm getting this result when running an rspec (v1.2.9) test against my code. the spec produces a file with a © symbol in it, and then reads the file back in to check the contents.

I'm running this in Ruby 1.9.2 at the moment, but I also need to support all the way back to Ruby 1.8.6. This is a Windows environment with RubyInstaller.org versions of Ruby.

like image 344
Derick Bailey Avatar asked Mar 02 '11 04:03

Derick Bailey


People also ask

How do I read a UTF-8 file?

In Java, the InputStreamReader accepts a charset to decode the byte streams into character streams. We can pass a StandardCharsets. UTF_8 into the InputStreamReader constructor to read data from a UTF-8 file.

How do you create a text file with UTF-8 encoding in Python?

Use file. write() to write UTF-8 text to a file In a with-as statement, call open(file, mode, encoding="utf-8") with mode as "w" to open file for writing in UTF-8 encoding. Call file. write(data) to write the text contained in data to the opened file . with open("sample.

How do I open a UTF-8 file in Python?

Use open() to open a file with UTF-8 encoding Call open(file, encoding=None) with encoding as "UTF-8" to open file with UTF-8 encoding.


1 Answers

If i execute your code i get an error on the special character. Can you try this code ?

# encoding: UTF-8 File.open("test.txt", "w:UTF-8") do |f|    f.write "test \u00A9 foo"  end   #Encoding.filesystem = "UTF-8" p Encoding.find("filesystem")  File.open("test.txt", "r:UTF-8") do |f|    puts f.read  end  

On my windows box i then get

#<Encoding:Windows-1252> test © foo 

I have no idea why the  is there..

like image 156
peter Avatar answered Oct 23 '22 14:10

peter