如何在 Ruby 中将字符串转换为 UTF8 [英] How to convert a string to UTF8 in Ruby
本文介绍了如何在 Ruby 中将字符串转换为 UTF8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个使用 Hpricot 的爬虫.它从某个网页下载字符串列表,然后我尝试将其写入文件.编码有问题:
I'm writing a crawler which uses Hpricot. It downloads a list of strings from some webpage, then I try to write it to the file. Something is wrong with the encoding:
"xC3" from ASCII-8BIT to UTF-8
我有在网页上呈现并以这种方式打印的项目:
I have items which are rendered on a webpage and printed this way:
Développement
str.encoding
返回 UTF-8
,所以 force_encoding('UTF-8')
没有帮助.如何将其转换为可读的 UTF-8?
the str.encoding
returns UTF-8
, so force_encoding('UTF-8')
doesn't help. How may I convert this to readable UTF-8?
推荐答案
您的字符串似乎编码错误:
Your string seems to have been encoded the wrong way round:
"Développement".encode("iso-8859-1").force_encoding("utf-8")
#=> "Développement"
这篇关于如何在 Ruby 中将字符串转换为 UTF8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文