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
frmongo
V2EX  ›  Python

中文转换

  •  
  •   frmongo · 2018-07-19 13:03:26 +08:00 · 1855 次点击
    这是一个创建于 2100 天前的主题,其中的信息可能已经有所发展或是发生改变。
    求助,类似这种字符串,如何转换为中文

    a = u'\u7f8e\u56feT9\u9890\u548c\u56ed\u9650\u91cf\u7248\uff086GB RAM/\u5168\u7f51\u901a\uff09'
    9 条回复    2018-07-19 17:17:53 +08:00
    ipwx
        1
    ipwx  
       2018-07-19 13:05:21 +08:00   ❤️ 1
    print(a)
    ipwx
        2
    ipwx  
       2018-07-19 13:05:47 +08:00
    或者

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

    In [6]: u'\u2665'.encode("utf8").decode('utf8')
    Out[6]: '♥'
    In [9]: '\u7f8e'.encode("gbk").decode('gbk')
    Out[9]: '美'
    chengyiqun
        7
    chengyiqun  
       2018-07-19 16:55:23 +08:00
    >>> print(a.encode('utf8').decode('utf8'))
    美图 T9 颐和园限量版( 6GB RAM/全网通)
    sikariba
        8
    sikariba  
       2018-07-19 17:05:26 +08:00
    存入 DB 的时候 encode('utf8')变成 bytes 类型,从 DB 读取的时候 decode('utf8')变成 str
    frmongo
        9
    frmongo  
    OP
       2018-07-19 17:17:53 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5519 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:59 · PVG 10:59 · LAX 19:59 · JFK 22:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.