gpt4 book ai didi

ruby - 编码::UndefinedConversionError

转载 作者:数据小太阳 更新时间:2023-10-29 06:24:12 27 4
gpt4 key购买 nike

每次尝试将哈希值转换为 JSON 字符串时,我都会收到 Encoding::UndefinedConversionError - "\xC2"from ASCII-8BIT to UTF-8 错误。我试过 [.encode | .force_encoding](["UTF-8"| "ASCII-8BIT"]),将 .encode.force_encoding 链接起来,向后,切换参数但没有似乎有效,所以我发现了这样的错误:

begin
menu.to_json
rescue Encoding::UndefinedConversionError
puts $!.error_char.dump
p $!.error_char.encoding
end

其中 menu 是续集的 dataset.to_hash,内容来自 MySQL 数据库,utf8_general_ci 编码并返回:

"\xC2"

<#Encoding:ASCII-8BIT>

编码永远不会改变,无论我使用什么.encode/.force_encoding。我什至尝试替换字符串 .gsub!(/\\\xC2/) 但没有成功。

有什么想法吗?

最佳答案

menu.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')

这非常有效,我不得不替换一些额外的字符,但没有更多的错误。

关于ruby - 编码::UndefinedConversionError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13003287/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com