Python の組み込み bytearray 関数の詳細な紹介
英文文档:
class bytearray
([ソース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 usingstr.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
[,エラー<span class="pre" style="margin: 0px; padding: 0px;">0 <span class="pre" style="margin: 0px; padding: 0px;"><= <span class="pre" style="margin: 0px; padding: 0px;">x <span class="pre" style="margin: 0px; padding: 0px;">< <span class="pre" style="margin: 0px; padding: 0px;">256</span></span></span></span></span>
[,エンコーディング
]]])
新しい配列を返しますバイト。 bytearray
クラスは、0 可変シーケンスのタイプで説明されている可変シーケンスの通常のメソッドのほとんどと、 bytes
型には、Bytes および Bytearray 操作を参照してください。
オプションsource
パラメータを使用して、いくつかの異なる方法で配列を初期化できます:🎜文字列🎜 の場合は、エンコーディング🎜 (およびオプションでエラー🎜) パラメータ;
bytearray()
は、str.encode()
.🎜🎜integer🎜 の場合、配列はそのサイズになり、null バイトで初期化されます。🎜🎜
padding: 0px;">buffer🎜 インターフェイスでは、オブジェクトの読み取り専用バッファがバイト配列の初期化に使用されます。🎜🎜
iterable🎜、
<span class="pre" style="margin: 0px;パディング: 0px;">0 <span class="pre" style="margin: 0px;パディング: 0px;"><= <span class="pre" style="margin: 0px;パディング: 0px;">x <span class="pre" style="margin: 0px;パディング: 0px;">< <span class="pre" style="margin: 0px; padding: 0px;">256</span></span></span></span></span>
、配列の初期内容として使用されます。🎜🎜🎜🎜引数なし、サイズ 0 の配列が作成されます。🎜🎜说明:🎜1. 戻り値は新しいバイト配列です
2. 3 つのパラメータのいずれも渡されない場合、長さ 0 のバイト配列が返されます
>>> b = bytearray() >>> b bytearray(b'') >>> len(b) 0
ログイン後にコピー3. ソースパラメータが文字列の場合、エンコーディングパラメータ 関数が str.encode メソッドを使用して文字列をバイト配列に変換することも指定する必要があります
>>> bytearray('中文') Traceback (most recent call last): File "<pyshell#48>", line 1, in <module> bytearray('中文') TypeError: string argument without an encoding >>> bytearray('中文','utf-8') bytearray(b'\xe4\xb8\xad\xe6\x96\x87')
ログイン後にコピー4. ソースパラメータが整数の場合、これで指定された長さの空のバイト配列を返しますinteger
>>> bytearray(2) bytearray(b'\x00\x00') >>> bytearray(-2) #整数需大于0,使用来做数组长度的 Traceback (most recent call last): File "<pyshell#51>", line 1, in <module> bytearray(-2) ValueError: negative count
ログイン後にコピー5. ソースパラメータがバッファインターフェイスを実装するオブジェクトである場合、バイトは読み取り専用モードでバイト配列に読み込まれ、ソースパラメータが返されます。が反復可能オブジェクトの場合、これは配列
に初期化できるように、反復可能オブジェクトの要素は 0>>> bytearray([1,2,3]) bytearray(b'\x01\x02\x03') >>> bytearray([256,2,3]) #不在0-255范围内报错 Traceback (most recent call last): File "<pyshell#53>", line 1, in <module> bytearray([256,2,3]) ValueError: byte must be in range(0, 256)
ログイン後にコピー
以上がPython の組み込み bytearray 関数の詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











LinuxターミナルでPythonバージョンを表示する際の許可の問題の解決策PythonターミナルでPythonバージョンを表示しようとするとき、Pythonを入力してください...

fiddlereveryversings for the-middleの測定値を使用するときに検出されないようにする方法

10時間以内にコンピューター初心者プログラミングの基本を教える方法は?コンピューター初心者にプログラミングの知識を教えるのに10時間しかない場合、何を教えることを選びますか...

PythonのPandasライブラリを使用する場合、異なる構造を持つ2つのデータフレーム間で列全体をコピーする方法は一般的な問題です。 2つのデータがあるとします...

UvicornはどのようにしてHTTPリクエストを継続的に聞きますか? Uvicornは、ASGIに基づく軽量のWebサーバーです。そのコア機能の1つは、HTTPリクエストを聞いて続行することです...

Investing.comの反クラウリング戦略を理解する多くの人々は、Investing.com(https://cn.investing.com/news/latest-news)からのニュースデータをクロールしようとします。
