使用 ruby 进行文件编码
我在文件编码方面遇到了一些问题。
我收到一个 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 &还有更多
I'm having a bit problems with file encodings.
I'm receiving a url-encoded string like "sometext%C3%B3+more+%26+andmore", unescape it, process the data, and save it with windows-1252 encoding.
The conversions are these:
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
And the result should be sometextó more & andmore
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ruby 1.9 中已添加编码支持,因此以下代码来自 Ruby 1.9.1:
此处 你有很多关于 Ruby 和编码的信息。
附言。 Ruby 1.8.7 没有内置的编码支持,因此您必须使用一些外部库进行转换,例如
Encoding support has been added to Ruby 1.9, so the following code is from Ruby 1.9.1:
Here you have lots of informations on Ruby and encodings.
PS. Ruby 1.8.7 doesn't have built-in support for encodings, so you have to use some external library for conversion, for example iconv: