您的位置:首页 > 其它

PyCharm 中文注释报错 SyntaxError: Non-ASCII character

2016-06-28 20:00 330 查看
Python菜鸟今天写程序时加了中文注释,竟然就报错了,头一回碰到注释报错,活久见。

平时看别人代码时,或多或少会碰到中文注释乱码问题,原因是不同文件编码格式,有些文件编码并不能显示中文,如ASCII。而PyCharm对于中文不仅仅是显示乱码问题,而是编译报错。如下

SyntaxError: Non-ASCII character '\xe6' in file TestPy3/t.py on line 19, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details


看看提示的链接 http://python.org/dev/peps/pep-0263/

Defining the Encoding
Python will default to ASCII as standard encoding if no other
encoding hints are given.

To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:

# coding=<encoding name>

or (using formats recognized by popular editors)

#!/usr/bin/python
# -*- coding: <encoding name> -*-

or

#!/usr/bin/python
# vim: set fileencoding=<encoding name> :


大概意思是,默认文件是ASCII格式,需要更改文件编码,操作是在文件首行加上

#!/usr/bin/python
# -*- coding:utf8 -*-


编译运行,一切正常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: