Python decode()メソッド

Python 字符串Python 文字列


説明

Python decode() メソッドは、encoding指定されたエンコーディング形式で文字列をデコードします。デフォルトのエンコーディングは文字列のエンコーディングです。

構文

decode()メソッドの構文:

str.decode(encoding='UTF-8',errors='strict')

パラメータ

  • encoding -- 使用するエンコーディング、例:"UTF-8"。

  • errors -- さまざまなエラー処理方法を設定します。デフォルトは 'strict' で、エンコードエラーが発生すると UnicodeError が発生します。その他の指定可能な値には、'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace'、および codecs.register_error() で登録された任意の値があります。

戻り値

このメソッドはデコードされた文字列を返します。

例

以下の例はdecode()メソッドの使用例を示しています:

例(Python 3.0+)

#!/usr/bin/python str = "this is string example....wow!!!"; str = str.encode('base64','strict'); print "Encoded String: " + str; print "Decoded String: " + str.decode('base64','strict')

上記の例の出力結果は次のとおりです:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=

Decoded String: this is string example....wow!!!

Python 字符串Python 文字列

その他の拡張