实战

来源:互联网 发布:网络炒作大概花费 编辑:程序博客网 时间:2024/04/28 03:32

如果想要在python程序中打印中文,默认情况下,你只会收到错误。在ex1.py中加入打印中文的代码,改成如下:

print "Hello Python!"print "Hello Again."print "I like typing this."print "This is fun."print "Yay!Printing."print "I'd much rather you 'not'."print 'I "said" do not touch this.'print "哈喽!"

再运行,就会发现报出如下错误。

File "ex1.py", line 8SyntaxError: Non-ASCII character '\xe5' in file ex1.py on line 8, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

那么实在需要打印中文,只需要在.py文件的首行加入编码格式说明(#– coding:utf-8 –)就可以。代码如下:

#-- coding:utf-8 --print "Hello Python!"print "Hello Again."print "I like typing this."print "This is fun."print "Yay!Printing."print "I'd much rather you 'not'."print 'I "said" do not touch this.'print "哈喽!"