Python String decode() 方法

2025年1月5日 | 阅读 7 分钟

引言

在本教程中,我们将学习 Python 字符串 decode() 方法。Python 的字符串 decode() 方法使用已注册的编解码器对其编码进行解码。此函数可用于解码编码后的字符串并获取原始字符串。此函数基于编码和错误工作。有许多标准编码,如 Base64、ascii、utf_32、utf_16 等。根据指定的编码确定字符串。您可以使用不同的错误处理方案来配置此过程中的不同解决方案。最后,该方法返回解码后的字符串。

语法

Python 中字符串 decode() 方法的语法如下:

参数

Python 中字符串 decode() 方法的参数如下:

  • encoding: 指定应进行解码的编码。
  • error: 用于决定如何处理出现的错误,例如“strict”在发生异常时修复 Unicode 错误,以及“ignore”忽略发生的错误。

返回值

decode() 方法从编码后的字符串返回原始的给定字符串。

Python 中的字符串编码和解码

Python 的字符串 decode() 方法使用“utf_16”作为编码以获得可变长度。如果错误指定为“strict”,则会引发 UnicodeError。在下面的示例中,字符串“Hello everyone, Welcome to javatpoint”。使用 Encode() 函数创建并编码。现在,返回编码后的字符串,并在该字符串上调用 decode() 函数。使用 print() 函数输出并打印解码后的字符串。在这两种情况下,使用的编码都是“utf_16”,错误处理方式是“strict”。

程序代码

这里我们提供一个 Python 中字符串编码和解码的程序代码。代码如下:

输出

现在,我们运行上面的代码,找出 Python 中编码和解码后的字符串。输出如下:

The encoded string from the given string is:  b'\xff\xfeH\x00e\x00l\x00l\x00o\x00 \x00e\x00v\x00e\x00r\x00y\x00o\x00n\x00e\x00,\x00 \x00W\x00e\x00l\x00c\x00o\x00m\x00e\x00 \x00t\x00o\x00 \x00j\x00a\x00v\x00a\x00t\x00p\x00o\x00i\x00n\x00t\x00'
The decoded string from the given string is:  Hello everyone, Welcome to javatpoint

程序代码

这里,我们提供另一个 Python 中字符串编码和解码的程序代码。代码如下:

输出

现在,我们运行上面的代码,找出 Python 中编码和解码后的字符串。输出如下:

String encoded in base64 format:
b'Welcome to javatpoint'
Here the decoded string is:
Welcome to javatpoint

程序代码

Python 的字符串 decode() 方法使用“euc_kr”作为编码来区分字符的长度。当我们指定错误为“strict”时,会引发 UnicodeError。在下面的示例中,字符串“Hello everyone, Welcome to javatpoint”。使用 encode() 函数创建并编码。现在,获取编码后的字符串,调用该字符串的 decode() 函数,并使用 print() 函数输出并打印解码后的字符串。在这两种情况下,使用的编码都是“euc_kr”,错误处理方式是“strict”。这里我们提供一个 Python 中字符串编码和解码的程序代码。代码如下:

输出

现在,我们运行上面的代码,找出 Python 中编码和解码后的字符串。输出如下:

Here the encoded string is:  b'Hello everyone, Welcome to javatpoint'
Here the decoded string is:  Hello everyone, Welcome to javatpoint

程序代码

Python 的字符串 decode() 方法使用“utf_32”作为编码以获得可变长度。如果错误指定为“replace”,则会替换为替换标记。这用于 replace_errors() 函数。在下面的示例中,字符串是“Hello everyone, Welcome to javatpoint”。使用 Encode() 函数创建并编码。现在,获取编码后的字符串,调用该字符串的 decode() 函数,并使用 print() 函数输出并打印解码后的字符串。在这两种情况下,使用的编码都是“utf_32”,错误处理方式是“replace”。这里我们提供一个 Python 中字符串编码和解码的程序代码。代码如下:

输出

现在,我们运行上面的代码,找出 Python 中编码和解码后的字符串。输出如下:

The encoded string from the given string is:  b'\xff\xfe\x00\x00H\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00 \x00\x00\x00e\x00\x00\x00v\x00\x00\x00e\x00\x00\x00r\x00\x00\x00y\x00\x00\x00o\x00\x00\x00n\x00\x00\x00e\x00\x00\x00,\x00\x00\x00 \x00\x00\x00W\x00\x00\x00e\x00\x00\x00l\x00\x00\x00c\x00\x00\x00o\x00\x00\x00m\x00\x00\x00e\x00\x00\x00 \x00\x00\x00t\x00\x00\x00o\x00\x00\x00 \x00\x00\x00j\x00\x00\x00a\x00\x00\x00v\x00\x00\x00a\x00\x00\x00t\x00\x00\x00p\x00\x00\x00o\x00\x00\x00i\x00\x00\x00n\x00\x00\x00t\x00\x00\x00'
The decoded string from the given string is:  Hello everyone, Welcome to javatpoint

程序代码

Python 的字符串 decode() 方法使用“utf_32”作为编码以获得可变长度。如果错误指定为“backslashreplace”,它将使用“backslashreplace”错误。在下面的示例中,字符串是“Hello everyone, Welcome to javatpoint”。使用 encode() 函数创建并编码。现在,获取编码后的字符串,调用该字符串的 decode() 函数,并使用 print() 函数输出并打印解码后的字符串。在这两种情况下,使用的编码都是“utf_16_be”,错误处理方式是“backslashreplace”。这里我们提供一个 Python 中字符串编码和解码的程序代码。代码如下:

输出

现在,我们运行上面的代码,找出 Python 中编码和解码后的字符串。输出如下:

The encoded string from the given string is:  b'\x00H\x00e\x00l\x00l\x00o\x00 \x00e\x00v\x00e\x00r\x00y\x00o\x00n\x00e\x00,\x00 \x00W\x00e\x00l\x00c\x00o\x00m\x00e\x00 \x00t\x00o\x00 \x00j\x00a\x00v\x00a\x00t\x00p\x00o\x00i\x00n\x00t'
The decoded string from the given string is:  Hello everyone, Welcome to javatpoint

程序代码

Python 的字符串 decode() 函数将在不带任何参数的情况下使用并执行默认编码值。在下面的示例中,字符串是“Hello! Welcome to Tutorialspoint”。使用无参数的 encode() 函数创建并编码。现在,获取编码后的字符串,调用不带字符串参数的 decode() 函数,并使用 print() 函数输出并打印解码后的字符串。这里我们提供一个 Python 中字符串编码和解码的程序代码。代码如下:

输出

现在,我们运行上面的代码,找出 Python 中编码和解码后的字符串。输出如下:

Here the encoded string is:  b'Hello everyone, Welcome to javatpoint'
Here the decoded string is:  Hello everyone, Welcome to javatpoint

编码和解码字符串的应用

虽然编码和解码可以一起用于存储后端密码的简单应用程序,但它们也可以一起用于许多其他涉及数据安全的应用程序,例如密码学。

程序代码

这里我们提供一个 Python 中编码和解码字符串应用的程序代码。代码如下:

输出

现在,我们运行上面的代码,找出 Python 中编码和解码后的字符串。输出如下:

Password entered: Stacey
You are entering the wrong Password!!

Password entered: woods_stacey
You are successfully logged in to the page

结论

在本教程中,我们正在学习 Python 字符串 decode() 方法。在 Python 中编码和解码字符串可能看起来令人生畏,但它是一个简单的过程。首先,我们需要确定要用于给定字符串的编码类型。这可以通过检查字符串本身或查找提供此信息的任何信息或元数据来完成。当我们知道编码格式时,我们可以使用 Python 的内置模块 codec 来解码给定的字符串。