V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2017-08-16 08:01:43 +08:00 · 3706 次点击
    这是一个创建于 2444 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

    3 条回复    2017-08-16 08:25:15 +08:00
    Librazy
        1
    Librazy  
       2017-08-16 08:14:10 +08:00
    urllib.parse.unquote
    pimin
        2
    pimin  
       2017-08-16 08:22:45 +08:00
    直接把%删了就好了,正什么则
    flamepeak
        3
    flamepeak  
    OP
       2017-08-16 08:25:15 +08:00
    @Librazy 谢谢,非常感谢。
    我是这么做的:
    Python2
    print (urllib.unquote('%BB%C6%B3%C9%CE%C4')).decode('gbk')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5463 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:23 · PVG 09:23 · LAX 18:23 · JFK 21:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.