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

python2.7 中的编码问题,关于中文的显示

  •  
  •   BruceYuan · 2016-02-25 15:03:45 +08:00 · 3613 次点击
    这是一个创建于 2954 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我在 python shell 中创建的文件为:

    # -*- coding: utf-8 -*-
    print '你好'

    然后 run module,可以成功显示为:
    >>>你好

    但是我在 pycharm 中如果写同样的东西,结果却是乱码.
    假如我把 print '你好' 改成 print u'你好' 就能正常显示,请问一下这是怎么回事啊?
    11 条回复    2016-02-25 22:24:44 +08:00
    Daniel65536
        1
    Daniel65536  
       2016-02-25 15:17:40 +08:00   ❤️ 1
    BruceYuan
        2
    BruceYuan  
    OP
       2016-02-25 15:52:27 +08:00
    @Daniel65536 感谢推荐,我好好看看
    WangYanjie
        3
    WangYanjie  
       2016-02-25 16:28:21 +08:00
    没用过 pycharm ,不过可以猜猜,方便的话贴一下乱码到底是什么吧!
    clino
        4
    clino  
       2016-02-25 16:36:20 +08:00
    把文件拖进 chrome 然后切换不同的编码浏览你就明白了
    pzzrudlf
        5
    pzzrudlf  
       2016-02-25 16:41:32 +08:00
    为什么要显示中文?全部英文就可以了。
    BruceYuan
        6
    BruceYuan  
    OP
       2016-02-25 21:13:59 +08:00
    @WangYanjie 就是一些你不认识的字(个数未知)
    BruceYuan
        7
    BruceYuan  
    OP
       2016-02-25 21:18:16 +08:00
    @clino 编码是个复杂的问题,现在发现文件也有默认的编码格式
    BruceYuan
        8
    BruceYuan  
    OP
       2016-02-25 21:18:52 +08:00
    @pzzrudlf 有的时候爬取中文资料呢?
    VYSE
        9
    VYSE  
       2016-02-25 21:51:47 +08:00
    # -*- coding: utf-8 -*- 只是告诉 PYTHON 你的代码是用 UTF-8 编码的而已,代码用其他编码只能读成乱码

    Pycharm file encoding 配置里可以 project or 文件的编码
    BruceYuan
        10
    BruceYuan  
    OP
       2016-02-25 22:23:56 +08:00
    @VYSE 感谢
    wentian
        11
    wentian  
       2016-02-25 22:24:44 +08:00
    Python2.x 的中文问题无法理解就只能是初级 Python 程序员
    贴一篇博客助楼主一下
    https://segmentfault.com/a/1190000004018799
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4027 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 05:16 · PVG 13:16 · LAX 22:16 · JFK 01:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.