# 示例代码:使用 decode 方法将字节串解码为字符串
# 定义一个字节串
byte_string = b'Hello, World!'
# 使用 decode 方法将其解码为字符串,默认使用 'utf-8' 编码
decoded_string = byte_string.decode()
# 输出解码后的字符串
print(decoded_string) # 输出: Hello, World!
# 如果指定其他编码方式,例如 'ascii'
decoded_string_ascii = byte_string.decode('ascii')
# 输出解码后的字符串
print(decoded_string_ascii) # 输出: Hello, World!
decode() 方法用于将字节串(bytes 类型)转换为字符串(str 类型)。默认情况下,它使用 'utf-8' 编码进行解码。decode() 方法中指定编码方式,例如 'ascii'、'gbk' 等。UnicodeDecodeError 异常。上一篇:python中//的用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站