python脚本中中文问题

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 details
w00228598@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> 



你可能感兴趣的:(python运维)