Hướng dẫn how to decode a string python - làm thế nào để giải mã một chuỗi python

Cải thiện bài viết

Lưu bài viết

Decode () là một phương thức được chỉ định trong các chuỗi trong Python 2. Phương thức này được sử dụng để chuyển đổi từ một sơ đồ mã hóa, trong đó chuỗi đối số được mã hóa thành sơ đồ mã hóa mong muốn. Điều này hoạt động đối diện với mã hóa. Nó chấp nhận mã hóa của chuỗi mã hóa để giải mã nó và trả về chuỗi gốc.
This method is used to convert from one encoding scheme, in which argument string is encoded to the desired encoding scheme. This works opposite to the encode. It accepts the encoding of the encoding string to decode it and returns the original string.

Cú pháp: Giải mã (mã hóa, lỗi)decode(encoding, error)

Các tham số: Mã hóa: Chỉ định mã hóa trên cơ sở giải mã phải được thực hiện.Error: Quyết định cách xử lý các lỗi nếu chúng xảy ra, ví dụ: ’nghiêm ngặt làm tăng lỗi Unicode trong trường hợp ngoại lệ và‘ bỏ qua các lỗi đã xảy ra.
encoding : Specifies the encoding on the basis of which decoding has to be performed.
error : Decides how to handle the errors if they occur, e.g ‘strict’ raises Unicode error in case of exception and ‘ignore’ ignores the errors occurred.

Trả về: Trả về chuỗi gốc từ chuỗi được mã hóa. Returns the original string from the encoded string.

& nbsp; mã số 1: mã để giải mã chuỗi
Code #1 : Code to decode the string

str = "geeksforgeeks"

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
7
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
8

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Str.decode(encoding='UTF-8',errors='strict')
0

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6
Str.decode(encoding='UTF-8',errors='strict')
3
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
8

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Str.decode(encoding='UTF-8',errors='strict')
6
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
3
Str.decode(encoding='UTF-8',errors='strict')
8
Str.decode(encoding='UTF-8',errors='strict')
9
#!/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')
0

Output:

The encoded string in base64 format is :  Z2Vla3Nmb3JnZWVrcw==

The decoded string is :  geeksforgeeks

Ứng dụng: Mã hóa và giải mã cùng nhau có thể được sử dụng trong các ứng dụng đơn giản lưu trữ mật khẩu ở phía sau và nhiều ứng dụng khác như mật mã liên quan đến việc giữ bí mật thông tin. Một minh chứng nhỏ của ứng dụng mật khẩu được mô tả dưới đây.
Encoding and decoding together can be used in the simple applications of storing passwords in the back end and many other applications like cryptography which deals with keeping the information confidential.
A small demonstration of the password application is depicted below.

& NBSP; Mã số 2: Mã để chứng minh ứng dụng mã hóa mã hóa
Code #2 : Code to demonstrate application of encode-decode

#!/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')
1= "geeksforgeeks"

#!/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')
4=
#!/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')
6

#!/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')
4=
#!/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')
9
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
0
Str.decode(encoding='UTF-8',errors='strict')
8
Str.decode(encoding='UTF-8',errors='strict')
9
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
4

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
4= "geeksforgeeks"

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
7= "geeksforgeeks"

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6str2 str3 str4

str5str6== str9

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
0
Str.decode(encoding='UTF-8',errors='strict')
8
Str.decode(encoding='UTF-8',errors='strict')
9=3

=4

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6=7=8

=9 "geeksforgeeks"0

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6"geeksforgeeks"3=8

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5"geeksforgeeks"6"geeksforgeeks"7=8

"geeksforgeeks"9=

#!/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')
6

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6str2 str3 str_enc 6

str5str_enc 8== str9

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
0
Str.decode(encoding='UTF-8',errors='strict')
8
Str.decode(encoding='UTF-8',errors='strict')
9=3

=4

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6=7=8

=9 "geeksforgeeks"0

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6"geeksforgeeks"3=8

"geeksforgeeks"9=

#!/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')
6

Output:

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!



Sự mô tả

Phương thức chuỗi Python Decode () giải mã chuỗi bằng cách sử dụng codec đã đăng ký để mã hóa. Nó mặc định là mã hóa chuỗi mặc định.decode() decodes the string using the codec registered for encoding. It defaults to the default string encoding.

Cú pháp

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

Thông số

  • Mã hóa - Đây là mã hóa được sử dụng. Để biết danh sách tất cả các sơ đồ mã hóa, vui lòng truy cập: mã hóa tiêu chuẩn. − This is the encodings to be used. For a list of all encoding schemes please visit: Standard Encodings.

  • Lỗi - Điều này có thể được đưa ra để đặt sơ đồ xử lý lỗi khác. Mặc định cho các lỗi là 'nghiêm ngặt', có nghĩa là các lỗi mã hóa làm tăng unicodeerror. Các giá trị có thể khác là 'bỏ qua', 'thay thế', 'xmlcharrefreplace', 'backslashreplace' và bất kỳ tên nào khác được đăng ký qua codecs.register_error (). − This may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' and any other name registered via codecs.register_error().

Giá trị trả về

Chuỗi được giải mã.

Thí dụ

#!/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')

Kết quả

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!

python_strings.htm

UTF là gì

UTF-8 là một trong những mã hóa được sử dụng phổ biến nhất và Python thường mặc định sử dụng nó. UTF là viết tắt của định dạng chuyển đổi Unicode, và '8' có nghĩa là các giá trị 8 bit được sử dụng trong mã hóa.one of the most commonly used encodings, and Python often defaults to using it. UTF stands for “Unicode Transformation Format”, and the '8' means that 8-bit values are used in the encoding.

Mục đích của phương thức mã hóa () và giải mã () là gì?

Các hàm mã hóa python () và giải mã ().Các phương thức mã hóa và giải mã của Python được sử dụng để mã hóa và giải mã chuỗi đầu vào, sử dụng mã hóa đã cho.to encode and decode the input string, using a given encoding.