IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Python error:SyntaxError: Non-ASCII character

    Adamhuan发表于 2017-05-26 03:59:27
    love 0

    在Python的编程过程中,你可能会遇到如题所示的错误。

    关于该错误的具体场景如下所示:

    [root@redis python]# cat helloworld.py 
    #!/usr/bin/python
    
    print "你好,世界 - Adamhuan"
    [root@redis python]# 
    [root@redis python]# python helloworld.py 
      File "helloworld.py", line 3
    SyntaxError: Non-ASCII character '\xe4' in file helloworld.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
    [root@redis python]# 
    [root@redis python]#

    增加对字符集编码方式的声明就可以了:

    [root@redis python]# cat helloworld.py 
    #coding=utf8
    #!/usr/bin/python
    
    print "你好,世界 - Adamhuan"
    [root@redis python]# 
    [root@redis python]# python helloworld.py 
    你好,世界 - Adamhuan
    [root@redis python]#

    这么写也是可以的:

    [root@redis python]# cat helloworld.py 
    # -*- coding: utf8 -*-
    #!/usr/bin/python
    
    print "你好,世界 - Adamhuan"
    [root@redis python]# 
    [root@redis python]# python helloworld.py 
    你好,世界 - Adamhuan
    [root@redis python]#

    ——————————————
    Done。



沪ICP备19023445号-2号
友情链接