转换unicode混乱,以正确的字符在Ruby? [英] Convert unicode mess to correct characters in Ruby?

查看:171
本文介绍了转换unicode混乱,以正确的字符在Ruby?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如:

"MÃ\u0083¼LLER".encoding
#<Encoding:UTF-8>   

"MÃ\u0083¼LLER".inspect    
"\"MÃ\\u0083¼LLER\""  

我能做些什么来挽救这样一个字符串?考虑到我没有原始数据。

What can I do to salvage such a string? Take into consideration I do not have the original data. Is this salvageable?

推荐答案

看起来字符串从utf-8转换为latin-1 两次。尝试这一些你的一些数据,让我知道如果它的工作:

Looks like the string was converted from utf-8 to latin-1 twice. Try this on some of your data and let me know if it worked:

require 'iconv'

def decode(str)
  i = Iconv.new('LATIN1','UTF-8')
  i.iconv(i.iconv(str)).force_encoding('UTF-8')
end

decode("MÃ\u0083¼LLER")
#=> "MüLLER"

这篇关于转换unicode混乱,以正确的字符在Ruby?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆