Python编码问题

python脚本默认以ASCII编码运行的,如果源码中有保存中文或其他非ASCII编码则会报错:

SyntaxError: Non-ASCII character ‘\xe5’ in file kNN.py on line 24, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

这时候需要在开头中加入文件注释说明: # -*- coding: UTF-8 -*- 或者开头以这样的新式写:

1
2
#!env python
#coding=utf-8