Python では、decode() が Strings で指定されるメソッドです。このメソッドは、引数文字列が目的のエンコード スキームにエンコードされる、1 つのエンコード スキームから変換するために使用されます。これはエンコードとは逆に機能します。エンコード文字列のエンコードを受け入れてデコードし、元の文字列を返します。 弦 。
Python Decode() 関数の構文
構文: デコード(エンコード、エラー)
パラメーター:
- エンコーディング: デコードを実行する際のベースとなるエンコードを指定します。
- エラー : エラーが発生した場合の処理方法を決定します。たとえば、「strict」は例外の場合に Unicode エラーを発生させ、「ignore」は発生したエラーを無視します。
- 戻り値 : エンコードされた文字列から元の文字列を返します。
Python で文字列をエンコードおよびデコードする
上記のコードはエンコードとデコードの例です。ここでは、まず UTF-8 を使用して文字列をエンコードし、次にそれをデコードすると、入力で与えたものと同じ出力文字列が得られます。
Python3
最後のコミット git を削除する
として選択
# initializing string> String>=> 'geeksforgeeks'> > encoded_string>=> String.encode(>'utf-8'>)> print>(>'The encoded string in base64 format is :'>)> print>(encoded_string)> > decoded_string>=> encoded_string.decode(>'utf-8'>)> print>(>'The decoded string is :'>)> print>(decoded_string)> |
>
>
出力:
ノートパソコンのキーボードのInsertキーはどこにありますか
The encoded string in base64 format is : b'geeksforgeeks' The decoded string is : geeksforgeeks>
エンコード・デコードの応用
エンコードとデコードを併用すると、パスワードをバックエンドに保存する単純なアプリケーションや、情報の機密保持を扱う暗号化などの他の多くのアプリケーションで使用できます。パスワード アプリケーションの簡単なデモンストレーションを以下に示します。
Python3
arp-a コマンド
import> base64> > user>=> 'geeksforgeeks'> passw>=> 'i_lv_coding'> > # Converting password to base64 encoding> passw_encoded>=> base64.b64encode(passw.encode(>'utf-8'>)).decode(>'utf-8'>)> > user_login>=> 'geeksforgeeks'> > # Wrongly entered password> pass_wrong>=> 'geeksforgeeks'> > print>(>'Password entered:'>, pass_wrong)> > if> pass_wrong>=>=> base64.b64decode(passw_encoded).decode(>'utf-8'>):> >print>(>'You are logged in!'>)> else>:> >print>(>'Wrong Password!'>)> > print>()> > # Correctly entered password> pass_right>=> 'i_lv_coding'> > print>(>'Password entered:'>, pass_right)> > if> pass_right>=>=> base64.b64decode(passw_encoded).decode(>'utf-8'>):> >print>(>'You are logged in!'>)> else>:> >print>(>'Wrong Password!'>)> |
スパークチュートリアル
>
>
出力:
Password entered : geeksforgeeks Wrong Password!! Password entered : i_lv_coding You are logged in!!>
Python Decode() メソッドの仕組み?
次のフローチャートは、 パイソン デコード: