如何使用 Net::Http 下载包含 UTF-8 字符的文件?
How can I use Net::Http to download a file with UTF-8 characters in it?
我有一个应用程序,用户可以在其中上传持久保存到 S3 的基于文本的文件(xml、csv、txt)。其中一些文件非常大。需要对这些文件中的数据执行多种操作,因此我不是从 S3 读取它们并偶尔超时,而是将文件下载到本地,然后对它们进行松散操作。
这是我用来从 S3 下载文件的代码。 Upload
是我用来存储这些信息的AR模型的名称。该方法是上传模型的实例方法:
def download
basename = File.basename(self.text_file_name.path)
filename = Rails.root.join(basename)
host = MyFitment::Utility.get_host_without_www(self.text_file_name.url)
Net::HTTP.start(host) do |http|
f = open(filename)
begin
http.request_get(self.text_file_name.url) do |resp|
resp.read_body do |segment|
f.write(segment) # Fails when non-ASCII 8-bit characters are included.
end
end
ensure
f.close()
end
end
filename
end
因此您会看到加载失败的那一行。此代码以某种方式认为所有下载的文件都以 ASCII 8 位编码。我怎样才能:
1) 像这样检查远程文件的编码
2)下载并写入成功。
这是当前特定文件发生的错误:
Encoding::UndefinedConversionError: "\x95" from ASCII-8BIT to UTF-8
from /Users/me/code/myapp/app/models/upload.rb:47:in `write'
感谢您提供的任何帮助!
How can I: 1) Check the encoding of a remote file like that.
您可以检查响应的 Content-Type
header,如果存在,可能看起来像这样:
Content-Type: text/plain; charset=utf-8
如您所见,编码在那里指定。如果没有Content-Type header,或者没有指定字符集,或者指定的字符集不正确,那么你就无法知道文本的编码。有些宝石可以尝试猜测编码(准确度越来越高),例如rchardet
、charlock_holmes
,但为了完全准确,您必须在阅读文本之前知道编码。
This code somehow thinks all files that are downloaded are encoded in
ASCII 8-bit.
在ruby中,ASCII-8BIT
等同于binary
,这意味着Net::HTTP库只是给你一个包含一系列单字节的字符串,就这样了由您决定如何解释这些字节。
如果你想将这些字节解释为 UTF-8,那么你可以使用 String#force_encoding()
:
text = text.force_encoding("UTF-8")
例如,如果您想要对字符串进行一些正则表达式匹配,并且您想要匹配完整字符(可能是 multi-byte)而不是单个字节,则您可能想要这样做。
Encoding::UndefinedConversionError: "\x95" from ASCII-8BIT to UTF-8
使用String#encode('UTF-8')
将ASCII-8BIT转换为UTF-8不适用于ascii码大于127的字节:
(0..255).each do |ascii_code|
str = ascii_code.chr("ASCII-8BIT")
#puts str.encoding #=>ASCII-8BIT
begin
str.encode("UTF-8")
rescue Encoding::UndefinedConversionError
puts "Can't encode char with ascii code #{ascii_code} to UTF-8."
end
end
--output:--
Can't encode char with ascii code 128 to UTF-8.
Can't encode char with ascii code 129 to UTF-8.
Can't encode char with ascii code 130 to UTF-8.
...
...
Can't encode char with ascii code 253 to UTF-8.
Can't encode char with ascii code 254 to UTF-8.
Can't encode char with ascii code 255 to UTF-8.
Ruby 只是一次从 ASCII-8BIT 字符串中读取一个字节,并尝试将字节中的字符转换为 UTF-8。因此,虽然 128 在 multi-byte 字符序列的一部分时可能是 UTF-8 中的合法字节,但作为单个字节,128 不是合法的 UTF-8 字符。
至于将字符串写入文件,而不是这样:
f = open(filename)
...如果你想输出 UTF-8 到文件,你会写:
f = open(filename, "w:UTF-8")
默认情况下,ruby 使用 Encoding.default_external
的任何值来编码输出到文件。 default_external 编码是从您的系统环境中提取的,或者您可以明确设置它。
我有一个应用程序,用户可以在其中上传持久保存到 S3 的基于文本的文件(xml、csv、txt)。其中一些文件非常大。需要对这些文件中的数据执行多种操作,因此我不是从 S3 读取它们并偶尔超时,而是将文件下载到本地,然后对它们进行松散操作。
这是我用来从 S3 下载文件的代码。 Upload
是我用来存储这些信息的AR模型的名称。该方法是上传模型的实例方法:
def download
basename = File.basename(self.text_file_name.path)
filename = Rails.root.join(basename)
host = MyFitment::Utility.get_host_without_www(self.text_file_name.url)
Net::HTTP.start(host) do |http|
f = open(filename)
begin
http.request_get(self.text_file_name.url) do |resp|
resp.read_body do |segment|
f.write(segment) # Fails when non-ASCII 8-bit characters are included.
end
end
ensure
f.close()
end
end
filename
end
因此您会看到加载失败的那一行。此代码以某种方式认为所有下载的文件都以 ASCII 8 位编码。我怎样才能:
1) 像这样检查远程文件的编码 2)下载并写入成功。
这是当前特定文件发生的错误:
Encoding::UndefinedConversionError: "\x95" from ASCII-8BIT to UTF-8
from /Users/me/code/myapp/app/models/upload.rb:47:in `write'
感谢您提供的任何帮助!
How can I: 1) Check the encoding of a remote file like that.
您可以检查响应的 Content-Type
header,如果存在,可能看起来像这样:
Content-Type: text/plain; charset=utf-8
如您所见,编码在那里指定。如果没有Content-Type header,或者没有指定字符集,或者指定的字符集不正确,那么你就无法知道文本的编码。有些宝石可以尝试猜测编码(准确度越来越高),例如rchardet
、charlock_holmes
,但为了完全准确,您必须在阅读文本之前知道编码。
This code somehow thinks all files that are downloaded are encoded in ASCII 8-bit.
在ruby中,ASCII-8BIT
等同于binary
,这意味着Net::HTTP库只是给你一个包含一系列单字节的字符串,就这样了由您决定如何解释这些字节。
如果你想将这些字节解释为 UTF-8,那么你可以使用 String#force_encoding()
:
text = text.force_encoding("UTF-8")
例如,如果您想要对字符串进行一些正则表达式匹配,并且您想要匹配完整字符(可能是 multi-byte)而不是单个字节,则您可能想要这样做。
Encoding::UndefinedConversionError: "\x95" from ASCII-8BIT to UTF-8
使用String#encode('UTF-8')
将ASCII-8BIT转换为UTF-8不适用于ascii码大于127的字节:
(0..255).each do |ascii_code|
str = ascii_code.chr("ASCII-8BIT")
#puts str.encoding #=>ASCII-8BIT
begin
str.encode("UTF-8")
rescue Encoding::UndefinedConversionError
puts "Can't encode char with ascii code #{ascii_code} to UTF-8."
end
end
--output:--
Can't encode char with ascii code 128 to UTF-8.
Can't encode char with ascii code 129 to UTF-8.
Can't encode char with ascii code 130 to UTF-8.
...
...
Can't encode char with ascii code 253 to UTF-8.
Can't encode char with ascii code 254 to UTF-8.
Can't encode char with ascii code 255 to UTF-8.
Ruby 只是一次从 ASCII-8BIT 字符串中读取一个字节,并尝试将字节中的字符转换为 UTF-8。因此,虽然 128 在 multi-byte 字符序列的一部分时可能是 UTF-8 中的合法字节,但作为单个字节,128 不是合法的 UTF-8 字符。
至于将字符串写入文件,而不是这样:
f = open(filename)
...如果你想输出 UTF-8 到文件,你会写:
f = open(filename, "w:UTF-8")
默认情况下,ruby 使用 Encoding.default_external
的任何值来编码输出到文件。 default_external 编码是从您的系统环境中提取的,或者您可以明确设置它。