分类: Python/Ruby
2016-01-25 15:46:59
Unicode字符串可以用多种方式编码为普通字符串, 依照你所选择的编码(encoding):
1 #将Unicode转换成普通的Python字符串:"编码(encode)" 2 unicodestring = u"Hello world" 3 utf8string = unicodestring.encode("utf-8") 4 asciistring = unicodestring.encode("ascii") 5 isostring = unicodestring.encode("ISO-8859-1") 6 utf16string = unicodestring.encode("utf-16") 7 8 9 #将普通的Python字符串转换成Unicode: "解码(decode)" 10 plainstring1 = unicode(utf8string, "utf-8") 11 plainstring2 = unicode(asciistring, "ascii") 12 plainstring3 = unicode(isostring, "ISO-8859-1") 13 plainstring4 = unicode(utf16string, "utf-16") 14 15 assert plainstring1==plainstring2==plainstring3==plainstring4