您的位置:首页 > 编程语言 > Python开发

SyntaxError: Non-ASCII character Python中文处理

2012-10-25 20:47 573 查看
python的中文题目一向是困扰新手的头疼题目,这篇文章将给你具体地讲解一下这方面的常识。当然,几乎可以断定的是,在将来的版本中,python会彻底解决此题目,不消我们这么麻烦了。

先来看看python的版本:

>>> import sys

>>> sys.version

""2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)]""

(一)用记事本创建一个文件ChineseTest.py,默认ANSI:

s = "中文"

print s

测试一下瞧瞧:

E:\Project\Python\Test>python ChineseTest.py

File "ChineseTest.py", line 1

SyntaxError: Non-ASCII character ""\xd6"" in file ChineseTest.py on line 1, but noencodingdeclared; see http://www.pytho
n.org/peps/pep-0263.html for details

偷偷地把文件编码改成UTF-8:

E:\Project\Python\Test>python ChineseTest.py

File "ChineseTest.py", line 1

SyntaxError: Non-ASCII character ""\xe4"" in file ChineseTest.py on line 1, but noencodingdeclared; see http://www.pytho
n.org/peps/pep-0263.html for details

无济于事。。。

既然它供给了网址,那就看看吧。简单地浏览一下,终于知道若是文件里有非ASCII字符,须要在第一行或第二行指定编码声明。把ChineseTest.py文件的编码从头改为ANSI,并加上编码声明:

# coding=gbk

s = "中文"

print s

再试一下:

E:\Project\Python\Test>python ChineseTest.py

中文

正常咯:)

(二)看一看它的长度:

# coding=gbk

s = "中文"

print len(s)

成果:4。

s这里是str类型,所以策画的时辰一个中文相当于两个英文字符,是以长度为4。

我们如许写:

# coding=gbk

s = "中文"

s1 = u"中文"

s2 = unicode(s, "gbk") #省略参数将用python默认的ASCII来解码

s3 = s.decode("gbk") #把str转换成unicode是decode,unicode函数感化与之雷同

print len(s1)

print len(s2)

print len(s3)

成果:

2

2

2

(三)接着来看看文件的处理惩罚:建树一个文件test.txt,文件格局用ANSI,内容为:abc中文,用python来读取

# coding=gbk

print open("Test.txt").read()

成果:abc中文

把文件格局改成UTF-8:

成果:abc涓 枃

显然,这里须要解码:

# coding=gbk

import codecs

print open("Test.txt").read().decode("utf-8")

成果:abc中文

上方的test.txt我是用Editplus来编辑的,但当我用Windows自带的记事本编辑并存成UTF-8格局时,

运行时报错:

# coding=gbk

import codecs

print open("Test.txt").read().decode("utf-8")

本来,某些软件,如notepad,在保存一个以UTF-8编码的文件时,会在文件开端的处所插入三个不成见的字符(0 xEF 0 xBB 0 xBF,即BOM)。

是以我们在读取时须要本身去掉这些字符,python中的codecs module定义了这个常量:

# coding=gbk

import codecs

print open("Test.txt").read().decode("utf-8")

成果:abc中文

(四)一点遗留题目

在第二项目组中,我们用unicode函数和decode办法把str转换成unicode。为什么这两个函数的参数用"gbk"呢?

第一反响是我们的编码声明里用了gbk(# coding=gbk),但真是如许?

批改一下源文件:

# coding=utf-8

s = "中文"

print unicode(s, "utf-8")

运行,报错:

Traceback (most recent call last):

File "ChineseTest.py", line 3, in <module>

s = unicode(s, "utf-8")

UnicodeDecodeError: ""utf8"" codec can""t decode bytes in position 0-1: invalid data

显然,若是前面正常是因为两边都应用了gbk,那么这里我对峙了两边utf-8一致,也应当正常,不至于报错。

更进一步的例子,若是我们这里转换仍然用gbk:

# coding=utf-8

s = "中文"

print unicode(s, "gbk")

成果:中文

翻阅了一篇英文材料,它大致讲解了python中的print道理:

When Python executes a print statement, it simply passes the output to the operating system (using fwrite() or something like it), and some other program is responsible for actually displaying that output on the screen. For example, on Windows, it might be
the Windows console subsystem that displays the result. Or if you""re using Windows and running Python on a Unix box somewhere else, your Windows SSH client is actually responsible for displaying the data. If you are running Python in an xterm on Unix, then
xterm and your X server handle the display.

To print data reliably, you must know the encoding that this display program expects.

简单地说,python中的print直接把字符串传递给操纵体系,所以你须要把str解码成与操纵系同一致的格局。Windows应用CP936(几乎与gbk雷同),所以这里可以应用gbk。

最后测试:

# coding=utf-8

s = "中文"

print unicode(s, "cp936")

成果:中文

原文链接:http://www.byywee.com/page/M0/S739/739904.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐