首页 / 操作系统 / Linux / Eclipse 中运行包含中文的Python程序
今天eclipse中写了一小段程序,其中包含中文的输出和注释,运行的时候报错如下:SyntaxError: Non-ASCII character "/xe4" in file G:/eclipse-work/XMLRPC/src/rpcserver.py on line 19, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details 于是顺着错误消息给出的URL看看到底是为啥。打开后发现原来是《 PEP 0263 -- Defining Python Source Code Encodings <!--This HTML is auto-generated.DO NOT EDIT THIS FILE!If you are writing a newPEP, see http://www.python.org/dev/peps/pep-0001 for instructions and linksto templates.DO NOT USE THIS HTML FILE AS YOUR TEMPLATE!-->》,文章的大概意思如下:1.Python will default to ASCII as standard encoding if no other encoding hints are given.To define a source code encoding, a magic comment mustbe placed into the source files either as first or second line in the file, such as:
# coding=<encoding name>
or (using formats recognized by popular editors)
#!/usr/bin/python
# -*- coding: <encoding name> -*- 大概意思就是说你想使你的sourcefile 不默认使用ASCII作为编码方式,那你就要显示的指定编码方式,例如我想用UTF-8这种方式,那么我可以在source file的头上添加如下信息: # -*- coding: UTF-8 -*-
from xmlrpclib import ServerProxy
from os.path import join,isfile 这样我就指定了我的代码的编码方式,现在再运行的话应该就不会出现问题了 在eclipse中指定你代码的编码方案步骤如下:1.2. 这样之后,我们所写的代码的编码格式就是UTF-8,推荐UTF-8,理由如下文所示