我正在使用一个返回“字节字符串”(字节)的库,我需要将其转换为字符串。

这两者之间真的有区别吗?它们是如何关联的,我该如何进行转换?


当前回答

简单地说,想想我们的自然语言,如英语、孟加拉语、汉语等。在说话时,所有这些语言都发出声音。但即使我们听到了,我们能听懂所有的吗?-

答案通常是否定的。所以,如果我说我懂英语,这意味着我知道这些声音是如何被编码成一些有意义的英语单词的,我只是用同样的方式解码这些声音来理解它们。所以,其他语言也是如此。如果你知道它,你就有了那种语言的编码器-解码器包,如果你不知道它,你就没有这个。

数字系统也是如此。就像我们自己一样,我们只能用耳朵听声音,用嘴巴发声,计算机只能存储字节和读取字节。因此,某个应用程序知道如何读取字节并解释它们(比如要考虑多少字节才能理解任何信息),并且以相同的方式编写,以便其其他应用程序也能理解它。但是如果没有理解(编码器-解码器),所有写入磁盘的数据都只是字节串。

其他回答

简单地说,想想我们的自然语言,如英语、孟加拉语、汉语等。在说话时,所有这些语言都发出声音。但即使我们听到了,我们能听懂所有的吗?-

答案通常是否定的。所以,如果我说我懂英语,这意味着我知道这些声音是如何被编码成一些有意义的英语单词的,我只是用同样的方式解码这些声音来理解它们。所以,其他语言也是如此。如果你知道它,你就有了那种语言的编码器-解码器包,如果你不知道它,你就没有这个。

数字系统也是如此。就像我们自己一样,我们只能用耳朵听声音,用嘴巴发声,计算机只能存储字节和读取字节。因此,某个应用程序知道如何读取字节并解释它们(比如要考虑多少字节才能理解任何信息),并且以相同的方式编写,以便其其他应用程序也能理解它。但是如果没有理解(编码器-解码器),所有写入磁盘的数据都只是字节串。

Python语言包括str和bytes作为标准的“内置类型”。换句话说,它们都是类。我认为没有必要去解释为什么Python以这种方式实现。

尽管如此,str和bytes彼此非常相似。两者都有大部分相同的方法。下面的方法对于str类是唯一的:

casefold
encode
format
format_map
isdecimal
isidentifier
isnumeric
isprintable

以下方法对于bytes类是唯一的:

decode
fromhex
hex

字符串是串在一起的一堆项目。字节串是一个字节序列,比如b'\xce\xb1\xce\xac'表示“α”。字符串是一串字符,比如“α”。序列的同义词。

字节串可以直接存储在磁盘上,而字符串(字符串)不能直接存储在磁盘上。它们之间的映射是一种编码。

Assuming Python 3 (in Python 2, this difference is a little less well-defined) - a string is a sequence of characters, ie unicode codepoints; these are an abstract concept, and can't be directly stored on disk. A byte string is a sequence of, unsurprisingly, bytes - things that can be stored on disk. The mapping between them is an encoding - there are quite a lot of these (and infinitely many are possible) - and you need to know which applies in the particular case in order to do the conversion, since a different encoding may map the same bytes to a different string:

>>> b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'.decode('utf-16')
'蓏콯캁澽苏'
>>> b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'.decode('utf-8')
'τoρνoς'

一旦知道使用哪一个,就可以使用字节字符串的.decode()方法从它获得正确的字符串,如上所述。为了完整起见,字符串的.encode()方法采用相反的方式:

>>> 'τoρνoς'.encode('utf-8')
b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'

什么是Unicode?:

从根本上说,计算机只是处理数字。它们通过为每个字母和其他字符分配一个数字来存储它们。 ...... Unicode为每个字符提供了一个唯一的数字,无论什么平台,什么程序,什么语言。

So when a computer represents a string, it finds characters stored in the computer of the string through their unique Unicode number and these figures are stored in memory. But you can't directly write the string to disk or transmit the string on network through their unique Unicode number because these figures are just simple decimal number. You should encode the string to byte string, such as UTF-8. UTF-8 is a character encoding capable of encoding all possible characters and it stores characters as bytes (it looks like this). So the encoded string can be used everywhere because UTF-8 is nearly supported everywhere. When you open a text file encoded in UTF-8 from other systems, your computer will decode it and display characters in it through their unique Unicode number.

当浏览器从网络接收到编码为UTF-8的字符串数据时,它将把数据解码为字符串(假设浏览器采用UTF-8编码)并显示字符串。

在Python 3中,你可以将字符串和字节字符串相互转换:

>>> print('中文'.encode('utf-8'))
b'\xe4\xb8\xad\xe6\x96\x87'
>>> print(b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8'))
中文

总而言之,字符串是在计算机上显示给人类阅读的,字节字符串是用于存储到磁盘和数据传输的。