Encoding :: UndefinedConversionError:“\xE4”从ASCII-8BIT到UTF-8 [英] Encoding::UndefinedConversionError: "\xE4" from ASCII-8BIT to UTF-8

查看:1048
本文介绍了Encoding :: UndefinedConversionError:“\xE4”从ASCII-8BIT到UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取此 CSV文件使用 Net :: HTTP

File.open(file, "w:UTF-8") do |f|
  content = Net::HTTP.get_response(URI.parse(url)).body
  f.write(content)
end

再次阅读我的本地csv文件后,我有一些奇怪的输出。

After reading my local csv file again, i got some weird output.


Nationalit\xE4t;更改0-5

Nationalit\xE4t;Alter 0-5

我尝试将其编码为UTF-8,但是收到错误 Encoding :: UndefinedConversionError:\xE4从ASCII-8BIT到UTF-8

I tried to encode it to UTF-8, but got the error Encoding::UndefinedConversionError: "\xE4" from ASCII-8BIT to UTF-8

rchardet gem告诉我内容是 ISO-8859-2 。但转换为 UTF-8 将无法正常工作。

The rchardet gem tolds me the content is ISO-8859-2. But convert to UTF-8 will not work.

在一个正常的文本编辑打开它,我看到它正常编码。

After open it in a normal Texteditor, i see it normal encoded.

推荐答案

您可以使用 force_encoding

require 'net/http'

url = "http://data.linz.gv.at/katalog/population/abstammung/2012/auslg_2012.csv"
File.open('output', "w:UTF-8") do |f|
  content = Net::HTTP.get_response(URI.parse(url)).body
  f.write(content.force_encoding("UTF-8"))
end

但是,这将使你失去一些acentuation在将该.cvs文件

But this will make you lose some acentuation in your .cvs file

如果您真的确定您始终使用此URL作为输入,并且该文件将始终保持此编码,您可以执行

If you are deadly sure that you always will use this URL as input, and the file will always keep this encoding, you can do

# encoding: utf-8
require 'net/http'

url = "http://data.linz.gv.at/katalog/population/abstammung/2012/auslg_2012.csv"
File.open('output', "w:UTF-8") do |f|
  content = Net::HTTP.get_response(URI.parse(url)).body
  f.write(content.encode("UTF-8", "ISO-8859-15"))
end

但这只适用于此文件。

这篇关于Encoding :: UndefinedConversionError:“\xE4”从ASCII-8BIT到UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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