• 请不要在回答技术问题时复制粘贴 AI 生成的内容
finian
1D
V2EX  ›  程序员

如何比较靠谱地判断一段文本的编码格式

  •  
  •   finian · Dec 13, 2013 · 3170 views
    This topic created in 4625 days ago, the information mentioned may be changed or developed.
    有一段文本,编码未知,需要尽量解码出文本内容而不出现乱码,目前只要求中文不出现乱码即可,有什么靠谱的方法没?尝试用了ICU库(http://site.icu-project.org/),发现判断不是特别准确。
    2 replies    1970-01-01 08:00:00 +08:00
    cute
        1
    cute  
       Dec 13, 2013
    python下我使用的chardet.
    效果还不错.
    scalaview
        2
    scalaview  
       Dec 13, 2013
    chardet+1

    >>> import urllib
    >>> rawdata = urllib.urlopen('http://www.google.cn/').read()
    >>> import chardet
    >>> chardet.detect(rawdata)
    {'confidence': 0.98999999999999999, 'encoding': 'GB2312'}
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4611 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 09:52 · PVG 17:52 · LAX 02:52 · JFK 05:52
    ♥ Do have faith in what you're doing.