如何在Ruby中指定输出文件编码? [英] How to specify output file encoding in Ruby?
本文介绍了如何在Ruby中指定输出文件编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当使用文件#打开
时,如何设置文件的编码?
How can I set the encoding of a file when using a File#open
?
推荐答案
以下是以UTF-16LE编码输出文件的示例:
Here's an example that outputs a file in the UTF-16LE encoding:
open("data.txt", "w:UTF-16LE")
Ruby查看您正在编写的字符串的编码,并根据需要进行转码。以下是一个非常详细的博客文章,介绍了优秀实例的力学(见该部分称为默认外部和内部编码。
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").
这篇关于如何在Ruby中指定输出文件编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文