Unicode是计算机可以支持这个星球上多种语言的秘密武器。通过使用一个或者多个字节来表示一个字符的方法突破了ASCII的限制。Unicode可以表示超过90000个字符。
使用方式:a=u‘hello‘ #Unicode String
String的内建函数str()和chr()并没有升级来处理Unicode,新的内建函数unicode()和unichar()可以看做是Unicode版本的str()和chr()。
Codec是什么?
codec是COder/DECoder的首字母组合。他定义了文本和二进制的转换方式,支持4中耳熟能详的编码方式:ASCII、IOS8859-1。utf-8、utf-16
编码解码:encode()函数解决了编码问题,decode()函数解决了解码问题
可以通过下面一个简单的例子来进行进一步的了解。
‘‘‘ An example of reading and writing Unicode String:writes a Unicode String to a file in utf-8 and reads it back in ‘‘‘ CODEC=‘utf-8‘ FILE=‘unicode.txt‘ hello_out=u‘Hello World\n‘ bytes_out=hello_out.encode(CODEC) f=open(FILE,‘w‘) f.write(bytes_out) f.close() f=open(FILE,‘r‘) bytes_in=f.read() f.close() hello_in=bytes_in.decode(CODEC) print hello_in
运行该程序,我们会得到如下输出:Hello World 同时在文件系统会多一个unicode.txt的文件,里面的内容和输出的一致。
原文:http://www.cnblogs.com/itdyb/p/5380182.html