本文介绍了红宝石“编码":“xC3";从 ASCII-8BIT 到 UTF-8 (Encoding::UndefinedConversionError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tvdb 中的汉尼拔剧集中有奇怪的角色.

Hannibal episodes in tvdb have weird characters in them.

例如:

Œuf

红宝石吐了:

./manifesto.rb:19:in `encode': "xC3" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)
    from ./manifesto.rb:19:in `to_json'
    from ./manifesto.rb:19:in `<main>'

第 19 行是:

puts @tree.to_json

有没有办法处理这些非utf字符?我宁愿不替换它们,而是转换它们?还是无视他们?我不知道,感谢任何帮助.

Is there a way to deal with these non utf characters? I'd rather not replace them, but convert them? Or ignore them? I don't know, any help appreciated.

奇怪的部分是脚本可以通过 cron 正常工作.手动运行它会产生错误.

Weird part is that script works fine via cron. Manually running it creates error.

推荐答案

看来您应该为对象使用另一种编码.您应该为变量 @tree 设置正确的代码页,例如,使用 iso-8859-1 而不是 ascii-8bit 使用 @tree.force_encoding('ISO-8859-1').因为 ASCII-8BIT 仅用于二进制文件.

It seems you should use another encoding for the object. You should set the proper codepage to the variable @tree, for instance, using iso-8859-1 instead of ascii-8bit by using @tree.force_encoding('ISO-8859-1'). Because ASCII-8BIT is used just for binary files.

要查找 ruby​​ 的当前外部编码,请发出:

To find the current external encoding for ruby, issue:

Encoding.default_external

如果 sudo 解决了问题,则问题是在默认代码页(编码)中,因此要解决它,您必须通过以下任一方式设置正确的默认代码页(编码):

If sudo solves the problem, the problem was in default codepage (encoding), so to resolve it you have to set the proper default codepage (encoding), by either:

  1. 在 ruby​​ 中将编码更改为 utf-8 或其他适当的,请执行以下操作:

  1. In ruby to change encoding to utf-8 or another proper one, do as follows:

Encoding.default_external = Encoding::UTF_8

  • bash 中,grep 当前有效设置:

  • In bash, grep current valid set up:

    $ sudo env|grep UTF-8
    LC_ALL=ru_RU.UTF-8
    LANG=ru_RU.UTF-8
    

    然后在 .bashrc 中以类似的方式正确设置它们,但不完全使用 ru_RU 语言,例如:

    Then set them in .bashrc properly, in a similar way, but not exactly with ru_RU language, such as the following:

    export LC_ALL=ru_RU.UTF-8
    export LANG=ru_RU.UTF-8
    

  • 这篇关于红宝石“编码":“xC3";从 ASCII-8BIT 到 UTF-8 (Encoding::UndefinedConversionError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-19 06:25