You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python按书写语言生成Unicode字符列表并导出为TXT文件?

Python 获取各书写语言对应Unicode字符列表并保存为TXT文件

核心实现方案

用Python内置的unicodedata库就能搞定,无需额外装第三方包。它可以识别字符所属的书写系统(脚本类型),我们只要遍历全范围Unicode码点,按脚本分类收集字符,最后导出成TXT文件即可。

代码示例

import unicodedata
from collections import defaultdict

def collect_unicode_by_script():
    # 用字典按脚本分类存储字符
    script_chars = defaultdict(list)
    
    # 遍历所有有效Unicode码点(0到0x10FFFF是Unicode完整范围)
    for code_point in range(0x110000):
        try:
            char = chr(code_point)
            # 获取字符所属脚本
            script = unicodedata.script(char)
            # 过滤通用符号、继承字符和控制字符,只保留书写语言专属字符
            if script not in ('Common', 'Inherited') and not unicodedata.category(char).startswith('C'):
                script_chars[script].append(char)
        except ValueError:
            # 跳过无效码点
            continue
    
    # 按脚本导出为TXT文件
    for script, chars in script_chars.items():
        filename = f"{script}_unicode_chars.txt"
        with open(filename, 'w', encoding='utf-8') as f:
            f.write(''.join(chars))
        print(f"已生成:{filename}")

if __name__ == "__main__":
    collect_unicode_by_script()

关键细节说明

  • unicodedata.script(char):返回字符的脚本标识,比如Han对应汉字、Latin对应拉丁字母、Arabic对应阿拉伯文等
  • 过滤规则:排除通用符号、继承字符和控制字符,避免无关内容混入
  • 文件编码:必须用utf-8保存,否则会丢失Unicode字符信息

扩展方向

如果需要精准对应到具体语言(而非脚本),可以自己维护一份「语言-脚本」映射表,把脚本进一步关联到具体语言(比如Han脚本对应中文、日文汉字),再做二次分类。

内容的提问来源于stack exchange,提问作者Jun Ha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 15:05:18