Python内蔵データ構造--bytes、bytearray
3135 ワード
bytes、bytearray Python 3は2つの新しいタイプを導入しました bytes 不変バイトタイプ byrearray バイト配列 可変
文字列とbytes 文字列は文字からなる秩序化シーケンスであり、文字は符号化を用いて を理解することができる. bytesは、バイトからなる秩序化された可変シーケンス である. bytearrayはバイトからなる秩序化された可変シーケンス である.
符号化と復号化 文字列異なる文字セット符号化encodeに従ってバイトシーケンスbytesを返す
バイトシーケンス異なる文字セットに従ってdecode戻り文字列を復号する
butes定義定義 に0で埋め込む. に等価である. がコピーされる. b接頭辞定義を使用する 基本ASCIIが文字形式b’abc 9’ を使用することのみを許可する.は、b"x 41x 61" を16進数で表す.
bytes操作はstrタイプと類似しており、いずれも可変タイプであるため、方法の多くは同じである.ただbytesの方法で、入力はbytesで、出力はbytesです
類の方法bytes.fromhex(string) stringは、2文字の16進数の形式でなければなりません.「6162 6 a 6 b」、スペースは無視されます.
hex() は、16進数で表される文字列を返します.
インデックス b'abcde'[2]は、そのバイトに対応する数を返し、intタイプ
bytearray定義 bytearray()空bytearray bytearray(int)バイトのbytearrayを指定し、 に0で埋め込む bytearray(iterable_of_ints)->bytearray[0255]のintからなる反復可能オブジェクト byterart(string,encoding[,errors])->bytearray近似string.Encode()は、可変オブジェクト に戻ることができません. bytearray(bytes_or_buffer)は、1バイトシーケンスまたはbufferから可変bytearrayオブジェクト をコピーする. b接頭辞定義のタイプはbytesタイプ であることに留意されたい.
bytearray操作とbytesタイプの方法は同じです bytearray(b’abcdef’)replace(b’f’,b’k’) bytearray(b’abc’).find(b’b’)
クラスメソッドbytearray.fromhex(string) stringは、2文字の16進数の形式でなければなりません.’6162 6 a 6 b’,スペースは 無視される bytearray.fromhex(‘6162 09 6a 6b00’)
hex() は、16進数で表される文字列 を返す. bytearray(‘abc’.encode()).hex()
インデックス bytearray(b’abcef’)[2]このバイトに対応する数を返し、intタイプ append(int)末尾に要素 を追加 insert(index,int)指定インデックス位置に要素 を挿入する. extend(iterable_of_ints)は、現在のbytearray に反復可能な整数セットを追加する. pop(index=-1)指定したインデックスから要素を除去し、デフォルトでは末尾から を除去します. remove(value)最初のvalue除去が見つかり、ValueError異常が見つからない 注意:上記の方法でintタイプを使用する必要がある場合、値は[0,255] です. clear()クリアbytearray reverse()bytearrayを反転し、 をその場で修正する
encode(encoding="utf-8",errors="strict") -> str
bytes.decode(encoding="utf-8",errors="strict") -> str
bytearray.decode(encoding="utf-8".errors="strict") -> str
butes定義
bytes()
空bytes bytes(int)
バイトのbytesを指定し、bytes(iterable_of_ints) -> bytes[0,255]
のintからなる反復可能オブジェクトbytes(string, encoding[, errors]) -> bytes
はstring.encode()
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
バイトシーケンスまたはbufferから新しい可変bytesオブジェクトbytes操作
b'abcdef'.replace(b'f',b'k')
b'abc'.find(b'b')
bytes.fromhex('6162 09 6a 6b00')
'abc'.encode().hex()
bytearray
bytearray操作