python bytes對于剛接觸python的小伙伴來講,可能還是有點陌生!bytes是位元組序列,值得注意的是它有取值范圍:0 <= bytes <= 255;
一.bytes函式簡介
python bytes位元組序列有以下幾種使用方式:
""" bytes(iterable_of_ints) -> bytes bytes(string, encoding[, errors]) -> bytes bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer bytes(int) -> bytes object of size given by the parameter initialized with null bytes bytes() -> empty bytes object Construct an immutable of bytes from: - an iterable yielding integers in range(256) - a text string encoded using the specified encoding - any object implementing the buffer API. - an integer # (copied from class doc) """ # 1.定義空的位元組序列bytes bytes() -> empty bytes # 2.定義指定個數的位元組序列bytes,默認以0填充,不能是浮點數 bytes(int) -> bytes of size given by the parameter initialized with null bytes # 3.定義指定內容的位元組序列bytes bytes(iterable_of_ints) # 4.定義位元組序列bytes,如果包含中文的時候必須設定編碼格式 bytes(string, encoding[, errors]) -> immutable copy of bytes_or_buffer
回傳值 : 回傳一個新的位元組序列,位元組序列bytes有一個明顯的特征,輸出的時候最前面會有一個字符b標識,舉個例子:
b'\x64\x65\x66' b'i love you' b'shuopython.com'
凡是輸出前面帶有字符b標識的都是位元組序列bytes;
二.bytes函式使用
1.定義空的位元組序列bytes
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解憂 @Blog(個人博客地址): shuopython.com @WeChat Official Account(微信公眾號):猿說python @Github:www.github.com @File:python_bytes.py @Time:2020/2/25 21:25 @Motto:不積跬步無以至千里,不積小流無以成江海,程式人生的精彩需要堅持不懈地積累! """ if __name__ == "__main__": a = bytes() print(a) print(type(a))
輸出結果:
b'' <class 'bytes'>
2.定義指定個數的位元組序列bytes,默認以0填充,不能是浮點數
if __name__ == "__main__": b1 = bytes(10) print(b1) print(type(b1)) # bytes 通過 decode函式轉為 str型別 s1 = b1.decode() print("s1:",s1) print(type(s1))
輸出結果:
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' <class 'bytes'> s1: <class 'str'>
3.定義指定內容的位元組序列bytes,只能是整數型別的序列,否則例外
if __name__ == "__main__": # 正常輸出 b1 = bytes([1, 2, 3, 4]) >>> b'\x01\x02\x03\x04' # bytes位元組序列必須是 0 ~ 255 之間的整數,不能含有float型別 b1 = bytes([1.1, 2.2, 3, 4]) >>> TypeError: 'float' object cannot be interpreted as an integer # bytes位元組序列必須是 0 ~ 255 之間的整數,不能含有str型別 b1 = bytes([1, 'a', 2, 3]) >>> TypeError: 'str' object cannot be interpreted as an integer # bytes位元組序列必須是 0 ~ 255 之間的整數,不能大于或者等于256 b1 = bytes([1, 257]) >>> ValueError: bytes must be in range(0, 256)
4.定義個位元組序列bytes
if __name__ == "__main__": b1 = bytes('abc', 'utf-8') # 如果包含中文必須設定編碼格式 print(b1) print("***"*20) b2 = bytes(b'def') print(b2) print(type(b2)) print(id(b2)) print("***" * 20) b3 = b'\x64\x65\x66' print(b3) print(type(b3)) print(id(b3)) print("***" * 20) # result = True if b2 == b3 else False print("b == bb 的結果是 ",(b2 == b3)) print("b is bb 的結果是 ", (b2 is b3))
輸出:
b'abc' ************************************************************ b'def' <class 'bytes'> 2563018794448 ************************************************************ b'def' <class 'bytes'> 2563018794448 ************************************************************ b == bb 的結果是 True b is bb 的結果是 True
注意:
1.python is和==的區別 文章中有詳細介紹:==是python標準運算子中的比較運算子,用來比較判斷兩個物件的value(值)是否相等,例如下面兩個字串間的比較;
2.is也被叫做同一性運算子,這個運算子比較判斷的是物件間的唯一身份標識,也就是id是否相同;
3.如果bytes初始化含有中文的字串必須設定編碼格式,否則報錯:TypeError: string argument without an encoding,如下:
b = bytes("猿說python") >>> b = bytes("猿說python") >>> TypeError: string argument without an encoding
三.重點提醒
1.bytes位元組序列的取值范圍:必須是0~255之間的整數;
2.bytes位元組序列是不可變序列:bytes是不可變序列,即和str型別一樣不可修改,如果通過find()、replace()、islower()等函式修改,其實是創建了新的bytes、str物件,可以通過內置函式id()查看值 是否發生變化,示例如下:
if __name__ == "__main__": # 1.通過 replace 生成新的bytes位元組序列 b1 = bytes(b"abcdefg") print(b1) print(type(b1)) print(id(b1)) print("***" * 20) b2 = bytes.replace(b1,b"cd",b"XY") print(b2) print(type(b2)) print(id(b2)) print("***" * 20) # 2.bytes 是不可變序列,不能直接修改bytes的內容 b1[0] = b"ss" >>> TypeError: 'bytes' object does not support item assignment
輸出結果:
b'abcdefg' <class 'bytes'> 2264724270976 ************************************************************ b'abXYefg' <class 'bytes'> 2264707281104 ************************************************************
python除了bytes位元組序列之外,還有bytearray可變的位元組序列,具體區別在哪呢?我們后續繼續講解;
猜你喜歡:
1.python字典推導式
2.python串列推導式
3.python例外處理
4.python執行緒互斥鎖
轉載請注明:猿說Python ? python bytes函式
技術交流、商務合作請直接聯系博主 掃碼或搜索:猿說python
猿說python
微信公眾號 掃一掃關注
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/186837.html
標籤:Python
上一篇:Python
