How to specify output file encoding in Ruby?

2019-01-23 01:06发布

How can I set the encoding of a file when using a File#open?

3条回答
smile是对你的礼貌
2楼-- · 2019-01-23 01:36

That blog also has a bunch of great information about character encoding with Ruby, including a post about encoding with Ruby 1.8.

查看更多
3楼-- · 2019-01-23 01:46

Here's an example that outputs a file in the UTF-16LE encoding:

open("data.txt", "w:UTF-16LE")

Ruby looks at the encoding of the string you are writing, and transcodes as necessary. Here's a very detailed blog post describing mechanics with excellent examples (see the section called "The Default External and Internal Encodings").

查看更多
我命由我不由天
4楼-- · 2019-01-23 01:52

Here's the way to read pages with Japanese Shift JIS encoding:

  file = open(url, "r:Shift_JIS")
查看更多
登录 后发表回答