0

我在文件编码方面遇到了一些问题。

我收到一个 url 编码的字符串,如“sometext%C3%B3+more+%26+andmore”,取消转义,处理数据,并使用 windows-1252 编码保存。

转换如下:

irb(main) >> value
=> "sometext%C3%B3+more+%26+andmore"
irb(main) >> CGI::unescape(value)
=> "sometext\303\263 more & andmore"
irb(main) >> #Some code and saved into a file using open(filename, "w:WINDOWS-1252")
irb(main) >> # result in the file:
=> sometextĂ³ more & andmore

结果应该是sometextó more & andmore

4

1 回答 1

4

Ruby 1.9 添加了编码支持,因此以下代码来自 Ruby 1.9.1:

require 'cgi'
#=> true
s = "sometext%C3%B3+more+%26+andmore"
#=> "sometext%C3%B3+more+%26+andmore"
t = CGI::unescape s
#=> "sometext\xC3\xB3 more & andmore"
t.force_encoding 'utf-8' # telling Ruby that the string is UTF-8 encoded
#=> "sometextó more & andmore"
t.encode! 'windows-1252' # changing encoding to windows-1252
#=> "sometext? more & andmore"
# here you do whatever you want to do with windows-1252 encoded string

这里有很多关于 Ruby 和编码的信息。

PS。Ruby 1.8.7 没有对编码的内置支持,因此您必须使用一些外部库进行转换,例如iconv

require 'iconv'
#=> true
require 'cgi'
#=> true
s = "sometext%C3%B3+more+%26+andmore"
#=> "sometext%C3%B3+more+%26+andmore"
t = CGI::unescape s
#=> "sometext\303\263 more & andmore"
Iconv.conv 'windows-1252', 'utf-8', t
#=> "sometext\363 more & andmore"
# \363 is ó in windows-1252 encoding
于 2010-05-28T12:54:28.853 回答