How to specify output file encoding in Ruby?

天涯浪子 提交于 2019-11-28 22:29:55

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").

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

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

  file = open(url, "r:Shift_JIS")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!