开发者

Ruby, "incompatible character encodings: UTF-8 and CP852 (Encoding::CompatibilityError)"

Why

# encoding: utf-8
out=File.open("z\\test.txt", "a"开发者_如何学Go) 
out.puts "ç"  
out.close
out=File.open("z\\test.txt", "r")
puts out.read+"ś"

results in "incompatible character encodings: UTF-8 and CP852 (Encoding::CompatibilityError)"?


The comment at the beginning of your ruby file, only determined the source encoding, i.e. it tells ruby which encoding the ruby file is encoded in. It does not tell it which encoding the files you're opening are encoded in - for that it still uses the system's default encoding unless you specifically request another one.

Apparently your system's default encoding is CP852, so if you want to open a file using utf-8, you'll have to specify that encoding when opening the file (passing the :encoding => "utf-8" as an argument to File.open).


Your script works fine for me on my box.

Is the error coming from your terminal application or from Ruby?

My terminal app is set to use utf-8.

You can probably avoid this problem by explicitly supplying an encoding when opening your files. See http://www.ruby-doc.org/core/classes/File.html#M000069 and follow the links to IO::new.


This should explain a lot

# https://pl.wikipedia.org/wiki/Kodowanie_polskich_znak%C3%B3w
inp = "zale\xBFno\x9cci".force_encoding('Windows-1250')

# inp = File.open('content-win-1250.txt', :encoding => 'Windows-1250').read

inp = inp.encode('utf-8')

File.open("tmp.txt", "wb") do |out|
    out.write(inp)
end

# file 'tmp.txt contains "zależności"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜