当前位置:   article > 正文

python unicode转字符串_Python——在Unicode和普通字符串之间转换

pythonunicode转换为字符串

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/153747
推荐阅读
相关标签
  

闽ICP备14008679号