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

中文转换

  •  
  •   frmongo · Jul 19, 2018 · 2626 views
    This topic created in 3038 days ago, the information mentioned may be changed or developed.
    求助,类似这种字符串,如何转换为中文

    a = u'\u7f8e\u56feT9\u9890\u548c\u56ed\u9650\u91cf\u7248\uff086GB RAM/\u5168\u7f51\u901a\uff09'
    9 replies  •  2018-07-19 17:17:53 +08:00
    ipwx
    ipwx
    Jul 19, 2018❤️1
    1
    print(a)
    ipwx
    ipwx
    Jul 19, 2018
    2
    或者

    windows: print(a.encode('gbk'))
    linux: print(a.encode('utf-8'))
    imn1
    imn1
    Jul 19, 2018
    3
    这个字符串可以直接使用,而且是跨语言的,你的需求是什么?想在 IDE 看到汉字?
    frmongo
    frmongo
    OP
    Jul 19, 2018
    4
    @imn1 多谢,我的需求是将这个字符串写入数据库的话,读出来还是不是中文了
    torbrowserbridge
    torbrowserbridge
    Jul 19, 2018
    5
    哪位大佬帮看看这个问题 https://www.v2ex.com/t/472366
    imn1
    imn1
    Jul 19, 2018
    6
    @frmongo
    理论上这个字串在其他语言也能识别的,读出来处理没问题
    怕不保险可以这样转一次再存:

    In [6]: u'\u2665'.encode("utf8").decode('utf8')
    Out[6]: '♥'
    In [9]: '\u7f8e'.encode("gbk").decode('gbk')
    Out[9]: '美'
    C02TobNClov1Dz56
    C02TobNClov1Dz56
    Jul 19, 2018
    7
    >>> print(a.encode('utf8').decode('utf8'))
    美图 T9 颐和园限量版( 6GB RAM/全网通)
    sikariba
    sikariba
    Jul 19, 2018
    8
    存入 DB 的时候 encode('utf8')变成 bytes 类型,从 DB 读取的时候 decode('utf8')变成 str
    frmongo
    frmongo
    OP
    Jul 19, 2018
    9
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4820 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 01:52 · PVG 09:52 · LAX 17:52 · JFK 20:52
    ♥ Do have faith in what you're doing.