python中报中文编码异常,Non-ASCII ,but no encoding declared

  1. 异常信息:
  • SyntaxError: Non-ASCII character 'xe5' in file a.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
  • 意思为编码错误
  1. 解决方案:
  • 在文件头上加上如下语句:
  • #!/usr/bin/python
  • #-*-coding:utf-8-*-

--以下是真实的案例--

#!/usr/bin/python
#-*-coding:utf-8-*-

import pandas as pd
from operator import delitem
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model.logistic import LogisticRegression
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV

原文地址:https://www.cnblogs.com/qqhfeng/p/5241919.html