Python2 和 Python3 中的 bytes.decode()

Rud*_*koŭ 0 python arrays decode python-2.x python-3.x

在sqlalchemy的源代码中我看到以下内容

    val = cursor.fetchone()[0]
    if util.py3k and isinstance(val, bytes):
        val = val.decode()
Run Code Online (Sandbox Code Playgroud)

为什么我们只对 Python3 进行解码而不对 Python2 进行解码?

Cri*_*ati 5

Python 3中,“普通”字符串是Unicode (与Python 2相反,在 Python 2 中它们是(扩展ASCII(或ANSI))。根据[Python 3.Docs]:Unicode HOWTO - 字符串类型

\n
\n

从 Python 3.0 开始, language\xe2\x80\x99s str类型包含 Unicode 字符,这意味着使用 , 创建的任何"unicode rocks!"字符串\'unicode rocks!\'或三引号字符串语法创建的任何字符串都存储为 Unicode。

\n
\n

例子:

\n
    \n
  • 蟒蛇3

    \n
    \n
    >>> import sys\n>>> sys.version\n\'3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]\'\n>>>\n>>> b = b"abcd"\n>>> s = "abcd"\n>>> u = u"abcd"\n>>>\n>>> type(b), type(s), type(u)\n(<class \'bytes\'>, <class \'str\'>, <class \'str\'>)\n>>>\n>>> b.decode()\n\'abcd\'\n>>> s.decode()\nTraceback (most recent call last):\n  File "<stdin>", line 1, in <module>\nAttributeError: \'str\' object has no attribute \'decode\'\n>>> u.decode()\nTraceback (most recent call last):\n  File "<stdin>", line 1, in <module>\nAttributeError: \'str\' object has no attribute \'decode\'\n
    Run Code Online (Sandbox Code Playgroud)\n
    \n
  • \n
  • 蟒蛇2

    \n
    \n
    >>> import sys\n>>> sys.version\n\'2.7.10 (default, Mar  8 2016, 15:02:46) [MSC v.1600 64 bit (AMD64)]\'\n>>>\n>>> b = b"abcd"\n>>> s = "abcd"\n>>> u = u"abcd"\n>>>\n>>> type(b), type(s), type(u)\n(<type \'str\'>, <type \'str\'>, <type \'unicode\'>)\n>>>\n>>> b.decode()\nu\'abcd\'\n>>> s.decode()\nu\'abcd\'\n>>> u.decode()\nu\'abcd\'\n
    Run Code Online (Sandbox Code Playgroud)\n
    \n
  • \n
\n

val将作为str进一步传递(到_parse_server_version ) 。因为在Python 3中,bytesstr不同,因此会执行转换。

\n

您还可以检查[SO]: Passing utf-16 string to a Windows function (@CristiFati\'s answer)

\n