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

求助:关于 Python 编码的问题

  •  
  •   maiganne · 2017-03-05 19:43:33 +08:00 · 2785 次点击
    这是一个创建于 2603 天前的主题,其中的信息可能已经有所发展或是发生改变。
    从网页上提取了一段字符出来, x=u'\u7535\u8bdd\u89c6\u9891\u4f1a\u8bae\u64cd\u4f5c\u6d41\u7a0b',已知网页的编码是 gb2312 的方式,现在想看到 x 的中文是什么,怎么处理?
    8 条回复    2017-03-07 10:55:05 +08:00
    falseen
        1
    falseen  
       2017-03-05 19:54:22 +08:00
    如果是 python2 的话,直接 print(x)即可。如果是 python3 的话不会存在这个问题。
    maiganne
        2
    maiganne  
    OP
       2017-03-05 19:59:35 +08:00
    @falseen 谢谢, print x 果然可以显示出来,那怎么让 x 变成正常显示的字符串?
    falseen
        3
    falseen  
       2017-03-05 20:08:26 +08:00
    在 python2 中,字符串就是长这样的,没法改变。它其实就是一个正常的字符串,只是显示的是 utf-8 编码而已,你可以对它进行任何正常的操作。如果你是强迫症患者,一定要让它显示中文的话,那么只有用 python3 了。
    wolong
        4
    wolong  
       2017-03-05 20:37:41 +08:00
    懒得研究 python 2 的编码问题,所以转 3 了。
    dant
        5
    dant  
       2017-03-06 11:06:23 +08:00
    x 是 Unicode codepoint 序列( Python 2 中的类型是 unicode , Python 3 中的类型是 str )
    可以通过 x.encode() 转换为字节序列( Python 2 中的类型是 str , Python 3 中的类型是 bytes )
    chez
        6
    chez  
       2017-03-06 11:12:24 +08:00
    x.encode('utf-8')
    alex0721
        7
    alex0721  
       2017-03-06 12:03:36 +08:00
    x.encode('gbk') 吧....
    crazypig14
        8
    crazypig14  
       2017-03-07 10:55:05 +08:00
    电话视频会议操作流程 。。。。 utf8 的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2660 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:33 · PVG 23:33 · LAX 08:33 · JFK 11:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.