代码之家  ›  专栏  ›  技术社区  ›  brainfck

是否将拉丁1字符串转换为utf8?

  •  9
  • brainfck  · 技术社区  · 16 年前

    如何将包含拉丁文1字符的字符串转换为utf8?

    字符串是一个文档,由openuri打开并包含这些特殊字符。

    顺致敬意,

    2 回复  |  直到 16 年前
        1
  •  16
  •   Jonathan Feinberg    16 年前

    Iconv

    require 'iconv'
    i = Iconv.new('UTF-8','LATIN1')
    a_with_hat = i.iconv("\xc2")
    
        2
  •  4
  •   andre-r    16 年前

    require 'rubygems'
    require 'open-uri'
    require 'nokogiri'
    require 'iconv'
    
    file = open(your_uri)
    doc = Nokogiri::HTML(Iconv.conv('utf-8', 'latin1', file.readlines.join("\n")))
    doc.xpath(your_xpath)
    

    如果不确定uri使用的字符集,可以使用 file.charset 获取字符集而不是 'latin' .