推荐学习书目
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
uti6770werty
V2EX  ›  Python

请教 Python 如何识别出 String 字符串,是否含有因为编码格式错误,而造成的乱码?

  •  
  •   uti6770werty · Dec 27, 2019 · 5175 views
    This topic created in 2398 days ago, the information mentioned may be changed or developed.
    在获取网页内容时候,早期没有处理好页面的编码格式 Unicode 或者 GB2312 的问题,一些中文文字是乱码,现在已经把内容读入到 String 内,如何判断出是否有乱码呢?
    如:
    textStringA = '����'
    textStringB = '佽隴:蟯伎'

    最怕就是 textStringB 这种,是有效中文,但它确实不是真实要看到的内容。。。
    6 replies    2019-12-28 19:14:23 +08:00
    mrchi
        1
    mrchi  
       Dec 27, 2019
    这个不太好处理吧,建议早期就处理好
    delectate
        3
    delectate  
       Dec 27, 2019
    比如长度是 10,有 5 个或以上不在 gb2312 里,就可以视为乱码了。
    no1xsyzy
        4
    no1xsyzy  
       Dec 27, 2019
    还是早期处理吧,不然可能造成流解析失败,你连 string 都没得
    ClericPy
        5
    ClericPy  
       Dec 27, 2019
    我之前在别人的爬虫系统里干过类似的, 这种没啥好办法, 统计 TF-IDF 找出不常见字符, 做 AC 自动机检测吧
    不在乎性能的话, 自己用 Counter 找出频率比较高的, 做成一个正则检测吧
    uti6770werty
        6
    uti6770werty  
    OP
       Dec 28, 2019
    谢谢各位,思索 10 来天,也确实没什么好办法,把这些乱码逆转回想要的编码,那更是不可能。。。。。。
    重新再去获取页面,也不可能了,源站点已经把这些页面内容“过期就不放出来了”。。。。。。
    现在只能老老实实把乱码的部分做缺失值处理。。。。
    感谢各位热心解答~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5505 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 08:14 · PVG 16:14 · LAX 01:14 · JFK 04:14
    ♥ Do have faith in what you're doing.