UTF-8中中文字符的上下界是什么?

4 投票
1 回答
3169 浏览
提问于 2025-04-17 12:27

我想在Python中创建一个集合,里面包含所有中文字符的ord()值:

对于英文字符来说,类似的做法是:

english = set(range(ord('a'),ord('z') + 1 ) +
              range(ord('A'),ord('Z') + 1 ))

1 个回答

12

根据Unicode标准(第6.0版,第12.1节),

汉字在Unicode标准中主要分为七个大类,具体可以参考表12-2。

Table 12-2. Blocks Containing Han Ideographs

Block                                   | Range       | Comment
----------------------------------------+-------------+-----------------------------------------------------
CJK Unified Ideographs                  | 4E00–9FFF   | Common
CJK Unified Ideographs Extension A      | 3400–4DBF   | Rare
CJK Unified Ideographs Extension B      | 20000–2A6DF | Rare, historic
CJK Unified Ideographs Extension C      | 2A700–2B73F | Rare, historic
CJK Unified Ideographs Extension D      | 2B740–2B81F | Uncommon, some in current use
CJK Compatibility Ideographs            | F900–FAFF   | Duplicates, unifiable variants, corporate characters
CJK Compatibility Ideographs Supplement | 2F800–2FA1F | Unifiable variants

此外,还有一些汉字不在这些大类中:

Table 12-3. Small Extensions to the URO

Range     | Version | Comment
----------+---------+-------------------------------------------------
9FA6–9FB3 | 4.1     | Interoperability with HKSCS standard
9FB4–9FBB | 4.1     | Interoperability with GB 18030 standard
9FBC–9FC2 | 5.1     | Interoperability with commercial implementations
9FC3      | 5.1     | Correction of mistaken unification
9FC4–9FC6 | 5.2     | Interoperability with ARIB standard
9FC7–9FCB | 5.2     | Interoperability with HKSCS standard

如果你想用集合操作来构建这些汉字的序号值集合,可以这样做:

chinese = set(range(0x4E00, 0xA000) +
              range(0x3400, 0x4DC0) +
              range(0x20000, 0x2A6E0) +
              range(0x2A700, 0x2B740) +
              range(0x2B740, 0x2B820) +
              range(0xF900, 0xFB00) +
              range(0x2F800, 0x2FA20) +
              range(0x9FA6, 0x9FCC))

不过要注意,这个集合里有超过75000个字符,所以它可能不是最紧凑或最有效的数据结构。

另外,如果你坚持要对字面字符使用ord()函数,你需要使用32位的unicode字面量形式:

>>> ord(u'\U00002F800')
194560

撰写回答