杰瑞科技汇

python 转换为unicode

核心概念:字符串 vs. 字节

要理解 Python 中“字符串”和“字节”的区别:

  • 字符串:是内存中 Unicode 字符的抽象序列,它不关心编码,只关心字符本身,字符 "A"、"中"、"€"。
  • 字节:是原始的 8 位数据序列,它需要通过某种“编码”(如 UTF-8, GBK, ASCII)才能被解释成字符。

“转换为 Unicode” 的真正含义是:将一个字节序列(bytes),通过指定的编码规则,解码成一个字符串(str)。


Python 3 中的处理(推荐)

在 Python 3 中,这个概念非常清晰和简单。

  • str 类型:表示 Unicode 字符串。
  • bytes 类型:表示字节序列。

从字节串解码为 Unicode 字符串

当你从文件、网络或 API 接收到数据时,它们通常是 bytes 类型,你需要使用 .decode() 方法将其转换为 str

语法: bytes_string.decode(encoding='utf-8')

示例:

假设你有一个 UTF-8 编码的字节串。

# 1. 定义一个 UTF-8 编码的字节串
# 注意:b'' 表示这是一个字节串
utf8_bytes = b'Hello, \xe4\xb8\x96\xe7\x95\x8c!'  # "世界" 的 UTF-8 编码
# 2. 将字节串解码为 Unicode 字符串
# Python 3 默认使用 UTF-8,但显式指定是最佳实践
unicode_string = utf8_bytes.decode('utf-8')
print(f"原始字节串: {utf8_bytes}")
print(f"类型: {type(utf8_bytes)}")
print("-" * 20)
print(f"解码后的字符串: {unicode_string}")
print(f"类型: {type(unicode_string)}")
# 输出:
# 原始字节串: b'Hello, \xe4\xb8\x96\xe7\x95\x8c!'
# 类型: <class 'bytes'>
# --------------------
# 解码后的字符串: Hello, 世界!
# 类型: <class 'str'>

处理编码错误: 如果字节串的编码与你的解码方式不匹配,会抛出 UnicodeDecodeError,你可以使用 errors 参数来处理错误。

  • errors='strict' (默认): 遇到错误直接抛出异常。
  • errors='ignore': 忽略无法解码的字符。
  • errors='replace': 将无法解码的字符替换成一个占位符(通常是 )。
# 错误示例:用错误的编码解码
wrong_bytes = b'Hello, \xe4\xb8\x96\xe7\x95\x8c!'
try:
    wrong_bytes.decode('ascii') # '世界' 无法用 ASCII 表示
except UnicodeDecodeError as e:
    print(f"解码失败: {e}")
# 使用 replace 处理
replaced_string = wrong_bytes.decode('ascii', errors='replace')
print(f"替换后: {replaced_string}") # 输出: Hello, ��!
# 使用 ignore 处理
ignored_string = wrong_bytes.decode('ascii', errors='ignore')
print(f"忽略后: {replaced_string}") # 输出: Hello, ��!

从 Unicode 编码为字节串

这是相反的操作,使用 .encode() 方法。

my_unicode_string = "你好,Python!"
# 将字符串编码为 UTF-8 字节串
utf8_encoded = my_unicode_string.encode('utf-8')
print(f"原始字符串: {my_unicode_string}")
print(f"类型: {type(my_unicode_string)}")
print("-" * 20)
print(f"编码后的字节串: {utf8_encoded}")
print(f"类型: {type(utf8_encoded)}")
# 输出:
# 原始字符串: 你好,Python!
# 类型: <class 'str'>
# --------------------
# 编码后的字节串: b'\xe4\xbd\xa0\xe5\xa5\xbd\uff0cPython!'
# 类型: <class 'bytes'>

Python 2 中的处理(遗留代码)

在 Python 2 中,情况比较混乱,这也是 Python 3 改进的主要原因。

  • str 类型:表示字节序列,默认编码是 ASCII。
  • unicode 类型:表示 Unicode 字符串。

从字节串(str)解码为 Unicode

你需要使用 unicode() 函数或 .decode() 方法。

# Python 2
# 1. 定义一个字节串 (str)
# 在 Python 2 中, '...' 默认是 str
py2_str = 'Hello, \xe4\xb8\x96\xe7\x95\x8c!' # "世界" 的 UTF-8 编码
# 2. 将字节串解码为 Unicode
# 必须指定编码!
py2_unicode = py2_str.decode('utf-8')
# 或者使用 unicode() 函数
# py2_unicode = unicode(py2_str, 'utf-8')
print(f"原始字节串: {py2_str}")
print(f"类型: {type(py2_str)}")
print("-" * 20)
print(f"解码后的Unicode: {py2_unicode}")
print(f"类型: {type(py2_unicode)}")
# 输出:
# 原始字节串: Hello, 世界!
# 类型: <type 'str'>
# --------------------
# 解码后的Unicode: Hello, 世界!
# 类型: <type 'unicode'>

从 Unicode 编码为字节串

使用 .encode() 方法。

# Python 2
my_unicode_string = u"你好,Python!" # u'' 表示这是一个 unicode 字符串
# 将 Unicode 编码为字节串
encoded_str = my_unicode_string.encode('utf-8')
print(f"原始Unicode: {my_unicode_string}")
print(f"类型: {type(my_unicode_string)}")
print("-" * 20)
print(f"编码后的字节串: {encoded_str}")
print(f"类型: {type(encoded_str)}")
# 输出:
# 原始Unicode: 你好,Python!
# 类型: <type 'unicode'>
# --------------------
# 编码后的字节串: 你好,Python!
# 类型: <type 'str'>

最佳实践

  1. 优先使用 Python 3,它的字符串模型更清晰、更安全。
  2. 显式指定编码:永远不要依赖系统的默认编码(如 sys.getdefaultencoding()),在解码和编码时,都明确写出 encoding='utf-8',UTF-8 是目前事实上的标准。
  3. 在输入/输出处处理编码
    • 文件:打开文件时使用 encoding 参数。
      # Python 3
      with open('my_file.txt', 'r', encoding='utf-8') as f:
          content = f.read() # content 是 str (Unicode)
    • 网络请求:像 requests 库会自动处理响应的编码,将响应内容作为文本返回。
    • 数据库:确保数据库连接和表都使用 UTF-8 编码。
  4. 统一内部编码:在你的应用程序内部,所有处理逻辑都使用 Unicode 字符串(Python 3 的 str),只在需要将数据写入文件、发送到网络或存储到数据库时,才将其编码为字节。
操作 Python 3 (推荐) Python 2 (遗留)
字节 -> 字符串 b'...'.decode('utf-8') '...'.decode('utf-8')unicode('...', 'utf-8')
字符串 -> 字节 '...'.encode('utf-8') u'...'.encode('utf-8')
字符串类型 str (Unicode) unicode
字节类型 bytes str

希望这个详细的解释能帮助你完全理解在 Python 中如何处理 Unicode!

分享:
扫描分享到社交APP
上一篇
下一篇