Converting UTF8 to ANSI with Ruby

痞子三分冷 提交于 2019-12-03 02:06:16
ascii_str = yourUTF8text.unpack("U*").map{|c|c.chr}.join

assuming that your text really does fit in the ascii character set.

I finally managed to do it using iconv, I was just messing up the parameters. So, this is how you do it:


require 'iconv'

utf8_csv = File.open("utf8file.csv").read

# gotta be careful with the weird parameters order: TO, FROM !
ansi_csv = Iconv.iconv("LATIN1", "UTF-8", utf8_csv).join

File.open("ansifile.csv", "w") { |f| f.puts ansi_csv }

That's it!

I had a similar issue trying to generate CSV files from user-generated content on the server. I found the unidecoder gem which does a nice job of transliterating unicode characters into ascii.

Example:

"olá, mundo!".to_ascii                 #=> "ola, mundo!"
"你好".to_ascii                        #=> "Ni Hao "
"Jürgen Müller".to_ascii               #=> "Jurgen Muller"
"Jürgen Müller".to_ascii("ü" => "ue")  #=> "Juergen Mueller"

For our simple use case, this worked well.

Pivotal Labs has a great blog post on unicode transliteration to ascii discussing this in more detail.

Since ruby 1.9 there is an easier way:

yourstring.encode('ASCII')

To avoid problems with invalid (non-ASCII) characters you can ignore the problems:

yourstring.encode('ASCII', invalid: :replace, undef: :replace, replace: "_")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!