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

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

  •  
  •   finian · Dec 13, 2013 · 3281 views
    This topic created in 4713 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
    cute
    Dec 13, 2013
    1
    python下我使用的chardet.
    效果还不错.
    scalaview
    scalaview
    Dec 13, 2013
    2
    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   ·   Privacy   ·   Solana   ·   2480 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:54 · PVG 08:54 · LAX 16:54 · JFK 19:54
    ♥ Do have faith in what you're doing.