admin 管理员组

文章数量: 1184232


2024年3月11日发(作者:constitution of business)

decode用法

decode是一个常用的Python函数,用于将字符串转换为其他类

型,如Unicode字符串、Base64字符串、URL编码字符串等。下面对

其常用的使用方法进行介绍。

1. 将Unicode字符串转换为普通字符串

使用decode函数将Unicode编码的字符串转换为普通字符串:

```python

u_str = 'hello world'

b_str = u_('utf-8')

u_str_again = b_('utf-8')

print(u_str_again)

```

输出结果为:

```

hello world

```

2. 将Base64字符串转换为普通字符串

使用decode函数将Base64编码的字符串转换为普通字符串:

```python

import base64

b64_str = 'aGVsbG8gd29ybGQ='

b_str = base64.b64decode(b64_str)

- 1 -

str_again = b_('utf-8')

print(str_again)

```

输出结果为:

```

hello world

```

3. 将URL编码字符串转换为普通字符串

使用urllib库中的unquote函数将URL编码的字符串转换为普

通字符串:

```python

from import unquote

url_str = 'hello%20world'

str_again = unquote(url_str)

print(str_again)

```

输出结果为:

```

hello world

```

总结:

以上是decode函数的常用使用方法,掌握这些方法可以帮助我

- 2 -

们更好地处理字符串相关的任务。

- 3 -


本文标签: 字符串 方法 转换 使用 编码