Python bytes 函数 - Python零基础入门教程
目錄
- 一.bytes 函數簡介
- 二.bytes 函數使用
- 1.定義空的字節序列 bytes
- 2.定義指定個數的字節序列 bytes ,默認以 0 填充,不能是浮點數
- 3.定義指定內容的字節序列 bytes ,只能是整數類型的序列,否則異常
- 4.定義個字節序列 bytes
- 三.重點提醒
- 四.猜你喜歡
零基礎 Python 學習路線推薦 :?Python 學習目錄?>>?Python 基礎入門
Python bytes 對于剛接觸 Python 的小伙伴來講,可能還是有點陌生!bytes 是字節序列,值得注意的是它有取值范圍:0 <= bytes <= 255;凡是輸出前面帶有字符 b 標識的都是字節序列 bytes ;
一.bytes 函數簡介
Python bytes 字節序列有以下幾種使用方式:
""" bytes(iterable_of_ints) -> bytesbytes(string, encoding[, errors]) -> bytesbytes(bytes_or_buffer) -> immutable copy of bytes_or_bufferbytes(int) -> bytes object of size given by the parameter initialized with null bytesbytes() -> empty bytes objectConstruct 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(個人博客地址):?www.codersrc.com @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?之間的整數,不能大于或者等于256b1?=?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?Falseprint("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 可變的字節序列,具體區別在哪呢?我們后續繼續講解;
四.猜你喜歡
未經允許不得轉載:猿說編程 ? Python bytes 函數
總結
以上是生活随笔為你收集整理的Python bytes 函数 - Python零基础入门教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通信协议讲解
- 下一篇: Python 线程池 ThreadPoo