Byte(字節) 與 Bytearray(二進制數組)


bytes

bytes是Python 3中特有的(bytes及bytes;str是str),Python 2 里的數據是不區分bytes和str(str和bytes都是bytes;unicode是unicode)。

Python 2中

    >>> type(b'xxxxx')
    <type 'str'>
    >>> type('xxxxx')
    <type 'str'>

Python 3中

    >>> type(b'xxxxx')
    <class 'bytes'>
    >>> type('xxxxx')
    <class 'str'>

區別

  • Python3中,bytes是byte的序列,而str是unicode的序列。

  • str 使用encode方法轉化為 bytes

  • bytes通過decode轉化為str

str轉換成bytes:

    In [9]: str1='人生苦短,我用Python!'

    In [10]: type(str1)
    Out[10]: str

    In [11]: b=str1.encode()

    In [12]: b
    Out[12]:         b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!'

    In [13]: type(str1.encode())
    Out[13]: bytes
bytes轉換成str:

    In [22]: b
    Out[22]:        b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!'

    In [23]: type(b)
    Out[23]: bytes

    In [24]: b.decode()
    Out[24]: '人生苦短,我用Python!'

    In [25]: type(b.decode())
    Out[25]: str    

注意點:  

  在Python 2中由於不區分str和bytes所以可以直接通過encode()和decode()方法進行編碼解碼。而在Python 3中把兩者給分開了這個在使用中需要注意。

  實際應用中在互聯網上都是通過二進制進行傳輸,所以就需要將str轉換成bytes進行傳輸,而在接收中通過decode()解碼成我們需要的要進行處理的數據。這樣不管對方是什么字符編碼全部編碼轉成了bytes數據進行網絡傳輸,而本地我們接收,再按照我們使用的字符編碼進行解碼,這樣就不會亂碼。

Bytearray

  bytearray和bytes不一樣的地方在於,bytearray是可變的。

  和字符串一樣,字節類型也是不可變序列,而字節數組是可變版本的字節,它們的關系就相當於listtuple。由於和字符串一樣是序列類型,字節和字節數組可用的方法基本上一致。

官方文檔:
    英文文檔:

class bytearray([source[, encoding[, errors]]])

    Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of 
mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Bytearray Operations. The optional source parameter can be used to initialize the array in a few different ways: If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray() then converts the string to bytes using str.encode(). If it is an integer, the array will have that size and will be initialized with null bytes. If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array. If it is an iterable, it must be an iterable of integers in the range 0 <= x < 256, which are used as the initial contents of the array. Without an argument, an array of size 0 is created. 譯文: 返回一個新的字節數組。bytearray類是一個值域為0 <= x < 256的可變整數序列。它具有大多數可變序列的常用方法,這將在可變序列類型中進行描述,其大多方法與bytes類型相同,詳細參考
bytes和bytesarray操作。 可選source參數可通過幾種途徑來初始化數組: - 如果是字符串,必須添加encoding(和可選error項)參數,然后bytearray() 通過利用str.encode()將字符串轉化為字節 - 如果是整數,數組獲得其大小,並用空字節進行初始化 - 如果是迭代,它必須是一個值域為0 <= x < 256的整數迭代,用於初始化數組的內容 無參數時,則創建一個大小為0的數組。
In [26]: str1
Out[26]: '人生苦短,我用Python!'

In [28]: b1=bytearray(str1.encode())

In [29]: b1
Out[29]: bytearray(b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!')

In [30]: b1.decode()
Out[30]: '人生苦短,我用Python!'

In [31]: b1[:6]=bytearray('生命'.encode())

In [32]: b1
Out[32]: bytearray(b'\xe7\x94\x9f\xe5\x91\xbd\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!')

In [33]: b1.decode()
Out[33]: '生命苦短,我用Python!'

注意點:

  data = bytearray("str xxxxxxx",encoding='utf-8')

  bytearray轉成的bytes數據由於是數組格式,可以計算其長度:len(data);也可以在數據之后追加數據:data.append(32);

但追加的必須是數字,他內部規則默認把數字轉成字節(比對ASCII碼表)

轉載博客:http://www.cnblogs.com/cuchadanfan/p/5926069.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM