在unicode字符串中转换字节字符串

问题描述

我有这样的代码:

a = "u0432"
b = u"u0432"
c = b"u0432"
d = c.decode('utf8')

print(type(a), a)
print(type(b), b)
print(type(c), c)
print(type(d), d)

然后输出:

<class 'str'> в
<class 'str'> в
<class 'bytes'> b'\u0432'
<class 'str'> u0432

为什么在后一种情况下我看到的是字符代码,而不是字符?如何将 Byte 字符串转换为 Unicode 字符串,以便在输出时我看到的是字符而不是其代码?

Why in the latter case I see a character code, instead of the character? How I can transform Byte string to Unicode string that in case of an output I saw the character, instead of its code?


解决方案

在字符串(或 Python 2 中的 Unicode 对象)中,u 有一个特殊的含义,即这里来了一个 Unicode由它的 Unicode ID 指定的字符".因此 u"u0432" 将产生字符 в.

In strings (or Unicode objects in Python 2), u has a special meaning, namely saying, "here comes a Unicode character specified by it's Unicode ID". Hence u"u0432" will result in the character в.

b'' 前缀告诉你这是一个 8 位字节序列,并且 bytes 对象没有 Unicode 字符,所以 u 代码没有特殊意义.因此,b"u0432" 只是字节的序列 ,u,0,432.

The b'' prefix tells you this is a sequence of 8-bit bytes, and bytes object has no Unicode characters, so the u code has no special meaning. Hence, b"u0432" is just the sequence of the bytes ,u,0,4,3 and 2.

本质上,您有一个 8 位字符串,其中不包含 Unicode 字符,而是包含 Unicode 字符的规范.

Essentially you have an 8-bit string containing not a Unicode character, but the specification of a Unicode character.

您可以使用 unicode 转义编码器转换此规范.

You can convert this specification using the unicode escape encoder.

>>> c.decode('unicode_escape')
'в'

相关文章