为什么Rails 3认为xE2x80x89的意思是x80 x89 [英] Why does Rails 3 think xE2x80x89 means â x80 x89

查看:140
本文介绍了为什么Rails 3认为xE2x80x89的意思是x80 x89的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从utf-8页面抓取了一个字段:

I have a field scraped from a utf-8 page:

"O’Reilly"

并保存在yml文件中:

And saved in a yml file:

:name: "O\xE2\x80\x99Reilly"

(xE2x80x99是该撇号的正确的UTF-8表示形式)

(xE2x80x99 is the correct UTF-8 representation of this apostrophe)

但是,当我将值加载到哈希中并让其生成标记为utf-8的页面时,我得到:

However when I load the value into a hash and yield it to a page tagged as utf-8, I get:

OâReilly

我查找了字符â,该字符在UTF-16中编码为x00E2,当粘贴字符串时,字符x80和x89不可见,但出现在â之后.我认为这意味着我的应用输出的是三个UTF-16字符,而不是一个UTF-8.

I looked up the character â, which is encoded in UTF-16 as x00E2, and the characters x80 and x89 were invisible but present after the â when I pasted the string. I assume this means my app is outputting three UTF-16 characters instead of one UTF-8.

如何使rails将3字节的UTF-8代码解释为单个字符?

How do I make rails interpret a 3-byte UTF-8 code as a single character?

推荐答案

最终,这是由加载带有psych(在轨道中)的syck文件(由外部脚本生成)引起的.用syck加载解决了该问题:

Ultimately this was caused by loading a syck file (generated by an external script) with psych (in rails). Loading with syck solved the issue:

#in ruby environment
puts YAML::ENGINE.yamler => syck

#in rails
puts YAML::ENGINE.yamler => psych

#in webapp
YAML::ENGINE.yamler = 'syck'
a = YAML::load(file_saved_with_syck)
a[index][:name] => "O’Reilly"
YAML::ENGINE.yamler = 'psych'

这篇关于为什么Rails 3认为xE2x80x89的意思是x80 x89的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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