【问题标题】:File encodings with ruby使用 ruby​​ 进行文件编码
【发布时间】:2010-05-28 12:17:48
【问题描述】:

文件编码有点问题。

我收到一个 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

【问题讨论】:

    标签: ruby file-encodings


    【解决方案1】:

    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
    

    Here你有很多关于 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
    

    【讨论】:

    • 我什么都没说,但我需要一个使用 Ruby 1.8.7 的解决方案(但谢谢:))
    • 我的输入有一些问题,但这有效。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    相关资源
    最近更新 更多