使用 Ruby 将 UTF8 转换为 ANSI [英] Converting UTF8 to ANSI with Ruby
问题描述
我有一个 Ruby 脚本,可以在 Linux 机器上远程生成一个 UTF8 CSV 文件,然后通过 SFTP 将该文件传输到 Windows 机器.
I have a Ruby script that generates a UTF8 CSV file remotely in a Linux machine and then transfers the file to a Windows machine thru SFTP.
然后我需要用 Excel 打开这个文件,但 Excel 没有得到 UTF8,所以我总是需要在能够将 UTF8 转换为 ANSI 的文本编辑器中打开该文件.
I then need to open this file with Excel, but Excel doesn't get UTF8, so I always need to open the file in a text editor that has the capability to convert UTF8 to ANSI.
我希望使用 Ruby 以编程方式执行此操作并避免手动转换步骤.最简单的方法是什么?
I would love to do this programmatically using Ruby and avoid the manual conversion step. What's the easiest way to do it?
PS:我尝试使用 iconv 但没有成功.
PS: I tried using iconv but had no success.
推荐答案
ascii_str = yourUTF8text.unpack("U*").map{|c|c.chr}.join
假设您的文本确实适合 ascii 字符集.
assuming that your text really does fit in the ascii character set.
这篇关于使用 Ruby 将 UTF8 转换为 ANSI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!