Python 文字列 encode() メソッド
定義と使用法
encode() メソッドは指定されたエンコーディングで文字列をエンコードします。エンコーディングが指定されていない場合、UTF-8 が使用されます。
文法
string.encode(encoding=encoding, errors=errors)
パラメータ値
パラメータ | 説明 |
---|---|
encoding | 選択可能。文字列。使用するエンコーディングを定義します。デフォルトは UTF-8 です。 |
errors |
選択可能。文字列。エラー処理方法を定義します。有効な値は:
|
さらに例
例
これらの例では、ascii エンコーディングとエンコードできない文字を使用して、異なるエラーレスポンスを示します:
txt = "My name is Ståle" print(txt.encode(encoding="ascii",errors="backslashreplace")) print(txt.encode(encoding="ascii",errors="ignore")) print(txt.encode(encoding="ascii",errors="namereplace")) print(txt.encode(encoding="ascii",errors="replace")) print(txt.encode(encoding="ascii",errors="xmlcharrefreplace")) print(txt.encode(encoding="ascii",errors="strict"))