推荐学习书目
› 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 · 2507 views
    This topic created in 2992 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
        1
    ipwx  
       Jul 19, 2018   ❤️ 1
    print(a)
    ipwx
        2
    ipwx  
       Jul 19, 2018
    或者

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

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