[英]how to fix Encoding::UndefinedConversionError in ruby
When trying to write a string to a file I get this message:尝试将字符串写入文件时,我收到以下消息:
irb(main):011:0> IO.write("/tmp/a1", r1.body.to_s)
Encoding::UndefinedConversionError: "\xC2" from ASCII-8BIT to UTF-8
from (irb):11:in `write'
from (irb):11
irb(main):012:0>
What am I doing wrong?我究竟做错了什么?
I found a question like yours.我发现了一个像你这样的问题。 Your string is in some other encoding, most likely iso-8859-1, so you should run this to convert it:
您的字符串采用其他编码,很可能是 iso-8859-1,因此您应该运行它来转换它:
"\xC2".encode("iso-8859-1").force_encoding("utf-8") => "Ã" "\xC2".encode("iso-8859-1").force_encoding("utf-8") => "Ã"
See the original question on stackoverflow , the answer on the top right now seem to be usefull.请参阅stackoverflow 上的原始问题,现在顶部的答案似乎很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.