SyntaxError: Non-ASCII character '\xe5' in file ... but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
注意到该文件中的
<pre name="code" class="python"><pre name="code" class="python"><span style="font-family: Arial, Helvetica, sans-serif;"><pre name="code" class="python"></span><span style="font-family: Arial, Helvetica, sans-serif;"># encoding:utf-8</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><pre name="code" class="python"># -*- coding: utf-8 -*-</span>
不在文件的第一行,之前有人改文件的时候把语句插入到这一行之前了。
可以一个一个文件的修改,但因为这个项目在自己这里只是做demo用的,修改成本过高,太费时间了,因此寻找pycharm默认用utf-8打开的方式
因为需要添加这个注释的文件太多了,用python写了个批处理的小文件 addoneline.py,运行后可以把所在文件夹中所有的.py文件第一行添加编码
# -*- coding: utf-8 -*-:
"""
name: addoneline.py
Created on Sat May 9 20:58:32 2015
@author: vincent
"""
import os
# import sys
# line = sys.argv[1]
line = '# -*- coding: utf-8 -*-'
py_files = [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1] == '.py']
for filename in py_files:
if filename == 'addoneline.py':
continue
with open (filename, 'r') as f:
orig_f = f.read()
if 'utf-8' not in orig_f[0:20]:
new_f = line + '\n' + orig_f
with open (filename, 'w') as f:
f.write(new_f)