SyntaxError: Non-ASCII character '\xbf' in file

使用python2.7

from numpy import * #科学计算包
import operator   #运算符模块

def createDataSet():
      group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
      labels = ['A','A','B','B']
      return group,labels

运行出现错误

================= RESTART: C:\Users\asus\Desktop\2.1\kNN.py =================
>>> import kNN

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    import kNN
  File "C:\Users\asus\Desktop\2.1\kNN.py", line 1
SyntaxError: Non-ASCII character '\xbf' in file C:\Users\asus\Desktop\2.1\kNN.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
>>> 

这个错误主要是由于python2的编码默认是ASCII,你的文件里有中文就必须要用utf-8编码,只要在文件需要在文件开头标注

#coding=utf-8
#coding=utf-8
from numpy import * #科学计算包
import operator   #运算符模块

def createDataSet():
      group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
      labels = ['A','A','B','B']
      return group,labels

结果

================= RESTART: C:\Users\asus\Desktop\2.1\kNN.py =================
>>> import kNN
>>> group,labels=kNN.createDataSet()
>>> group
array([[1. , 1.1],
       [1. , 1. ],
       [0. , 0. ],
       [0. , 0.1]])
>>> labels
['A', 'A', 'B', 'B']
;