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

请教 Python GB2312 编码字符串转回汉字

  •  
  •   flamepeak · Aug 16, 2017 · 4554 views
    This topic created in 3223 days ago, the information mentioned may be changed or developed.

    服务器传过来的字符串是人名使用 gb2312 编码后的字符串,例如:%BB%C6%B3%C9%CE%C4,黄成文,现在我要用 Python 把这个字符串转回汉字,求教怎么办?

    网上看到有这种办法:正则匹配 \%[A-F0-9]{2} 然后取出对应的 16 进制字符串,转换为相应的字符编码,再求出对应字符,最后替换原字符~

    有没有更好的办法,或者大神给个函数,谢谢

    3 replies    2017-08-16 08:25:15 +08:00
    Librazy
        1
    Librazy  
       Aug 16, 2017
    urllib.parse.unquote
    pimin
        2
    pimin  
       Aug 16, 2017
    直接把%删了就好了,正什么则
    flamepeak
        3
    flamepeak  
    OP
       Aug 16, 2017
    @Librazy 谢谢,非常感谢。
    我是这么做的:
    Python2
    print (urllib.unquote('%BB%C6%B3%C9%CE%C4')).decode('gbk')
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1559 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 16:33 · PVG 00:33 · LAX 09:33 · JFK 12:33
    ♥ Do have faith in what you're doing.