推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
vtoexsir
V2EX  ›  Python

Python 可否检查某文本为某编码方式的几率?

  •  
  •   vtoexsir · Sep 9, 2017 · 3931 views
    This topic created in 3343 days ago, the information mentioned may be changed or developed.

    伪代码如下:
    s = '**************'
    print(detectRisk(s,'gbk') #=>80% s 是 gbk 编码的概率是 80%
    print(detectRisk(s,'utf8') #=>30% s 是 u8 编码的概率是 30%
    请问 python 有没有这样的方法呢?
    有 cchardet.dect(s)或者 chardet.dect(s),但都不能指定检查哪一种编码的概率。

    7 replies  •  2017-09-10 23:08:30 +08:00
    mskip
    mskip
    Sep 9, 2017
    2
    chardet
    {'confidence': 0.7525, 'language': '', 'encoding': 'utf-8'}
    {'confidence': 1.0, 'language': '', 'encoding': 'ascii'}
    {'confidence': 0.99, 'language': '', 'encoding': 'utf-8'}
    {'confidence': 0.99, 'language': '', 'encoding': 'utf-8'}
    vtoexsir
    vtoexsir
    OP
    Sep 9, 2017
    3
    @nullcc
    @mskip
    chardet 不能指定检查哪种编码的概率。比如指定一段文本检查 gbk 编码的概率是多少。
    ltux
    ltux
    Sep 9, 2017
    4
    那就自己实现喽。
    janxin
    janxin
    Sep 9, 2017❤️1
    5
    In [1]: import chardet

    In [2]: prober = chardet.utf8prober.UTF8Prober()

    In [3]: prober.feed('你好,世界!'.encode('utf-8'))
    Out[3]: 1

    In [4]: prober.get_confidence()
    Out[4]: 0.99
    param
    param
    Sep 9, 2017
    6
    chardet 有出错的,而且速度很慢
    yucongo
    yucongo
    Sep 10, 2017
    7
    s = '**************', 貌似少个 b 吧,s = b'**************'。str 是不存在编码的问题的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2958 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 12:07 · PVG 20:07 · LAX 04:07 · JFK 07:07
    ♥ Do have faith in what you're doing.