赞
踩
1.1. 问题 Problem
You need to deal with data that doesn't fit in the ASCII character set.
你需要处理不适合用ASCII字符集表示的数据.
1.2. 解决 Solution
Unicode strings can be encoded in plain strings in a variety of ways, according to whichever encoding you choose:
Unicode字符串可以用多种方式编码为普通字符串, 依照你所选择的编码(encoding):
Toggle line numbers
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
1.3. 讨论 Discussion
If you find yourself dealing with text that contains non-ASCII characters, you have to learn about Unicode梬hat it is, how it wor
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。