当在Python 2.X文件中写中文注释或输出中文时候,经常会出现编译错误(在Python 3.X中没有这种错误。):
SyntaxError: Non-ASCII character '\xe7' in file E:\EclipseWorkspace\ImoocCrawler\test\test_urllib2.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
这是因为Python 2.X的默认编码文件是用ASCII码,如果想要编译通过,则只需要在文件开头加入一行代码将文件存成UTF-8格式即可。代码为
#coding=utf-8或
# -*- coding:utf-8 -*-