Python3是怎样解决Python2中出现的字符集编码问题的

本文转发自: https://foofish.net/how-python3-handle-charset-encoding.html

python2.x的不足:

Python2.X中的字符集编码真的是一件很蛋疼的事情,主要的原因是:

  • 使用 ASCII 码作为默认编码方式,对中文处理很不友好。
  • 把字符串的牵强地分为 unicode 和 str 两种类型,误导开发者。
    1
    2
    3
    4
    5
    6
    7
    Python 2.7.12 (default, Nov 19 2016, 06:48:10)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.getdefaultencoding()
    'ascii'
    >>>

这都是大坑,只要不注意可能就会出现这样那样的错误。

python3.X中的改进:

首先,Python3 把系统默认编码设置为 UTF-8。

1
2
3
4
5
6
7
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
>>>

然后,文本字符和二进制数据区分得更清晰。

python3.X分别用 str 和 bytes 表示。

文本字符全部用 str 类型表示,str 能表示 Unicode 字符集中所有字符,而二进制字节数据用一种全新的数据类型,用 bytes 来表示。

str
1
2
3
4
5
6
7
8
9
10
11
12
13
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = "a"
>>> a
'a'
>>> type(a)
<class 'str'>
>>> b = "好"
>>> b
'好'
>>> type(b)
<class 'str'>
bytes

Python3 中,在字符引号前加‘b’,明确表示这是一个 bytes 类型的对象,实际上它就是一组二进制字节序列组成的数据,bytes 类型可以是 ASCII范围内的字符和其它十六进制形式的字符数据,但不能用中文等非ASCII字符表示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> c = b"a"
>>> c
b'a'
>>> type(c)
<class 'bytes'>
>>> d = b"\xe7\xa6\x85"
>>> d
b'\xe7\xa6\x85'
>>> type(d)
<class 'bytes'>
>>>
>>> e = b"好"
File "<stdin>", line 1
SyntaxError: bytes can only contain ASCII literal characters.
>>>

bytes 类型提供的操作和 str 一样,支持分片、索引、基本数值运算等操作。但是 str 与 bytes 类型的数据不能执行 + 操作,尽管在py2中是可行的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> b'a'+b'c'
b'ac'
>>> b'a'*2
b'aa'
>>> b'abcdef\x86'[1:]
b'bcdef\x86'
>>> b'abcdef\x86'[:3]
b'abc'
>>> b'abcdef\x86'[-1]
134
>>> b'a'+'b'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't concat bytes to str

1
2
3
4
5
6
7
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> b'a'+b'c'
'ac'
>>> b'a'+'b'
'ab'

python2.x和python3.x之间子节与字符对应的关系。

python2 python3 表现 转换 作用
str bytes 字节 encode 存储
unicode str 字符 decode 显示

在Python3.x中的转换:

encode 负责字符到字节的编码转换。默认使用 UTF-8 编码准换。
1
2
3
4
5
>>> s = "好日子"
>>> s.encode()
b'\xe5\xa5\xbd\xe6\x97\xa5\xe5\xad\x90'
>>> s.encode("gbk")
b'\xba\xc3\xc8\xd5\xd7\xd3'
decode 负责字节到字符的解码转换,通用使用 UTF-8 编码格式进行转换。
1
2
3
4
5
6
7
>>> s = b'\xe5\xa5\xbd\xe6\x97\xa5\xe5\xad\x90'
>>> s.decode()
'好日子'
>>> s.decode('gbk')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'gbk' codec can't decode byte 0x90 in position 8: incomplete multibyte sequence

不知道为什么,gbk格式的编码总是过不去。

未完待续…