破碎的UTF-8字符串ruby [英] broken UTF-8 String ruby

查看:209
本文介绍了破碎的UTF-8字符串ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在我的文件中有以下内容时,我在读取文件时得到破坏的UTF-8字符串错误

través



如果我将其更改为正常 e 那么它工作。



有什么方法可以解决这个问题?



错误只会发生在 .lstrp 或任何其他函数。只是打印行是确定。



问题甚至发生时,我试图匹配字符串与正则表达式。

解决方案

显然,您的文件不是UTF-8编码的。所以,你应该照顾(使用UTF-8保存你的文件),或者告诉Ruby你的字符串不会是UTF-8。要执行此操作,您可以




  • 使用 -E encoding 命令行使用 RUBYOPT 环境变量

    切换

  • 传递 -E encoding li>
  • 在Ruby注释中指定编码

  • force_encoding code> lstrp



在下面的博客上有很多Ruby编码相关的阅读: / p>

http://blog.grayproductions.net/ articles / understanding_m17n


While reading a file I get broken UTF-8 String error whenever I have the following in my file

través

if I change it to normal e then it works.

Whats the way to fix this?

error only happens if I do line.lstrp or any other function. Just printing the lines is ok.

problem even happens when I try to match the string with regex.

解决方案

Obviously your file is not UTF-8 encoded. So, you should either take care of that (save your file using UTF-8), or tell Ruby that your strings aren't going to be UTF-8. In order to do that, you can either

  • use -E encoding command line switch for that
  • pass -E encoding using RUBYOPT environment variable
  • specify encoding within Ruby comment
  • do force_encoding method on already loaded string before operation such as lstrp

There's a lot of Ruby encoding-related reading on following blog:

http://blog.grayproductions.net/articles/understanding_m17n

这篇关于破碎的UTF-8字符串ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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