杰瑞科技汇

Python中如何将Unicode字符转为int?

  1. 获取 Unicode 字符的 Unicode 码点(整数表示)
  2. 将表示数字的 Unicode 字符串转换为整数
  3. 处理包含非数字字符的 Unicode 字符串

获取 Unicode 字符的 Unicode 码点

这是最直接的理解方式,每个 Unicode 字符都有一个唯一的数字标识,称为“码点”(Code Point),字符 'A' 的码点是 65,字符 '中' 的码点是 20013

Python中如何将Unicode字符转为int?-图1
(图片来源网络,侵删)

在 Python 中,可以使用内置函数 ord() 来完成这个转换。

ord() 函数

ord(c) 接收一个长度为 1 的 Unicode 字符 c,返回它的整数(Unicode 码点)。

示例:

# 英文字符
char_a = 'A'
code_point_a = ord(char_a)
print(f"字符 '{char_a}' 的码点是: {code_point_a}")  # 输出: 字符 'A' 的码点是: 65
# 数字字符
char_5 = '5'
code_point_5 = ord(char_5)
print(f"字符 '{char_5}' 的码点是: {code_point_5}")  # 输出: 字符 '5' 的码点是: 53
# 中文字符
char_zh = '中'
code_point_zh = ord(char_zh)
print(f"字符 '{char_zh}' 的码点是: {code_point_zh}")  # 输出: 字符 '中' 的码点是: 20013
# 特殊符号
char_emoji = '😊'
code_point_emoji = ord(char_emoji)
print(f"字符 '{char_emoji}' 的码点是: {code_point_emoji}") # 输出: 字符 '😊' 的码点是: 128522

反向操作:chr()

Python中如何将Unicode字符转为int?-图2
(图片来源网络,侵删)

如果你有一个整数(码点),想把它转换回对应的 Unicode 字符,可以使用 chr() 函数。

num = 65
char = chr(num)
print(f"码点 {num} 对应的字符是: '{char}'") # 输出: 码点 65 对应的字符是: 'A'
num_zh = 20013
char_zh = chr(num_zh)
print(f"码点 {num_zh} 对应的字符是: '{char_zh}'") # 输出: 码点 20013 对应的字符是: '中'

将表示数字的 Unicode 字符串转换为整数

如果你有一个字符串,"123""42",并且你想要把它转换成整数 12342,你应该使用 int() 函数。

int() 函数

int(x) 可以将一个数字字符串 x 转换为整数。

示例:

Python中如何将Unicode字符转为int?-图3
(图片来源网络,侵删)
# 字符串 "123"
str_num = "123"
int_num = int(str_num)
print(f"字符串 '{str_num}' 转换为整数是: {int_num}") # 输出: 字符串 '123' 转换为整数是: 123
print(type(int_num)) # 输出: <class 'int'>
# 字符串 "007" (前导零会被忽略)
str_num_leading_zero = "007"
int_num_leading_zero = int(str_num_leading_zero)
print(f"字符串 '{str_num_leading_zero}' 转换为整数是: {int_num_leading_zero}") # 输出: 字符串 '007' 转换为整数是: 7
# 包含 Unicode 字符的数字字符串
str_num_unicode = "𝟏𝟐𝟑"  # 这是 Unicode 中的 "数学双线体数字"
# int(str_num_unicode)  # 这会直接报错 ValueError: invalid literal for int() with base 10: '𝟏𝟐𝟑'
# 因为 int() 函数无法识别这些特殊的数字字符形式。
# 正确的做法是先将其转换为普通数字字符
# 注意:这个转换可能需要自定义逻辑或第三方库,因为标准库没有直接提供。
# 你可以创建一个映射字典:
math_to_digit = {
    '𝟏': '1', '𝟐': '2', '𝟑': '3', '𝄞': '4', '𝟓': '5',
    '𝟔': '6', '𝟳': '7', '𝟖': '8', '𝟗': '9', '𝟘': '0'
}
converted_str = ''.join([math_to_digit.get(c, c) for c in str_num_unicode])
print(f"转换后的字符串: {converted_str}")
int_num_unicode = int(converted_str)
print(f"转换后的整数: {int_num_unicode}") # 输出: 转换后的整数: 123

处理包含非数字字符的 Unicode 字符串

当一个字符串中包含非数字字符时,直接使用 int() 会失败,这时,你需要先对字符串进行“清理”或“提取”。

方法1:使用 str.isdigit()str.isdecimal() 过滤

isdigit()isdecimal() 可以判断一个字符是否为数字。isdecimal() 更严格,只接受 0-9isdigit() 还接受一些其他 Unicode 数字字符(如上例的 )。

示例:

mixed_str = "abc123xyz456"
# 提取所有数字字符
numeric_chars = [c for c in mixed_str if c.isdigit()]
numeric_str = "".join(numeric_chars)
print(f"原始字符串: {mixed_str}")
print(f"提取的数字字符: {numeric_str}") # 输出: 提取的数字字符: 123456
# 转换为整数
if numeric_str:
    final_int = int(numeric_str)
    print(f"最终转换的整数: {final_int}") # 输出: 最终转换的整数: 123456
else:
    print("字符串中没有找到数字。")

方法2:使用正则表达式

正则表达式是处理复杂字符串模式的强大工具,使用 re.findall() 可以非常灵活地提取数字。

示例:

import re
mixed_str = "价格是 1,000 元,折扣 20%。"
# 提取所有连续的数字
# \d 匹配任何 Unicode 数字字符(包括 𝟏 这种)
# + 表示匹配一个或多个
numbers_str_list = re.findall(r'\d+', mixed_str)
print(f"原始字符串: {mixed_str}")
print(f"找到的数字字符串列表: {numbers_str_list}") # 输出: 找到的数字字符串列表: ['1', '000', '20']
# 将列表中的字符串转换为整数列表
numbers_int_list = [int(num_str) for num_str in numbers_str_list]
print(f"转换后的整数列表: {numbers_int_list}") # 输出: 转换后的整数列表: [1, 0, 20]

总结与对比

你的目标 函数/方法 示例 输出
获取字符的 Unicode 码点 ord() ord('A') 65
将整数转换为字符 chr() chr(65) 'A'
将数字字符串转为整数 int() int("123") 123
从混合字符串中提取数字 列表推导 + isdigit() [c for c in "a1b2" if c.isdigit()] ['1', '2']
从混合字符串中提取数字 re.findall() re.findall(r'\d+', "a1b2c3") ['1', '2', '3']

关键点:

  • ord() 是处理单个字符到其码点的桥梁。
  • int() 是处理数字字符串数值的桥梁。
  • int() 遇到非数字字符时,会抛出 ValueError,需要先用其他方法(如 isdigit() 或正则表达式)进行数据清洗。

根据你的具体需求,选择最合适的方法即可。

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