python程序中有中文_python脚本中中文问题

当Python脚本中包含中文字符时,会遇到编码错误。错误提示为'Non-ASCII character'。解决方法是在文件头部添加指定编码的注释,如'# coding=utf-8'。本文列举了三种解决方案,并提供了一个包含中文的示例脚本。
摘要由CSDN通过智能技术生成

python /home/w00228598/python_test20160729/duoxc.py

运行duoxc.py脚本,报错:

w00228598@linux02:~/python_test20160729> python duoxc.py

File "duoxc.py", line 17

SyntaxError: Non-ASCII character '\xe7' in file duoxc.py on line 17, but no encoding declared; see http://www.python.org/peps/pep-0263.html for detailsw00228598@linux02:~/python_test20160729>

可按照错误建议网址查看http://www.python.org/peps/pep-0263.html

发现是因为Python在默认状态下不支持源文件中的编码所致。解决方案有如下三种:

一、在文件头部添加如下注释码:

# coding= 例如,可添加# coding=utf-8

二、在文件头部添加如下两行注释码:

#!/usr/bin/python

# -*- coding: -*- 例如,可添加# -*- coding: utf-8 -*-

三、在文件头部添加如下两行注释码:

#!/usr/bin/python

# vim: set fileencoding= : 例如,可添加# vim: set fileencoding=utf-8 :

附脚本:

w00228598@linux02:~/python_test20160729> cat duoxc.py  #!/usr/bin/python # coding=utf-8 import time import threading def music(func):     for i in range(2):         print "I was listening to %s. %s"% (func,time.ctime()) time.sleep(2) def movie(func):     for i in range(2): print "I was watching to %s. %s"% (func,time.ctime()) time.sleep(5) threads = [] t1 = threading.Thread(target=music,args=(u'爱情买卖',)) threads.append(t1) t2 = threading.Thread(target=movie,args=(u'阿凡达',)) threads.append(t2) if __name__ == '__main__':     for t in threads: t.setDaemon(True) t.start() print "all is over. %s"% time.ctime() w00228598@linux02:~/python_test20160729>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值