繁体   English   中英

Python、unicodedata 名称和代码点值,我缺少什么?

[英]Python, unicodedata name, and codepoint value, what am i missing?

我正在处理 unicode 数据字符,我想知道为什么有些在 unicodedata 中没有任何名称? 这是一个示例代码,您可以在其中检查< unknown >

我认为 unicode 数据库中的每个字符都被命名了,顺便说一句,所有相同的类别都是[Cc] Other, Control

另一个问题:如何获取 unicode 代码点值? 它是ord(unicodechar)吗?

我也把文件放在这里(因为编码是一件很奇怪的事情),因为我认为我用“隐形”字符剪切的粘贴可能是有损的。

#!/bin/env python
# -*- coding: utf-8 -*-

#extracted and licensing from here:
"""
:author: Laurent Pointal <laurent.pointal@limsi.fr> <laurent.pointal@laposte.net>
:organization: CNRS - LIMSI
:copyright: CNRS - 2004-2009
:license: GNU-GPL Version 3 or greater
:version: $Id$
"""

# Chars alonemarks:
#         !?¿;,*¤@°:%|¦/()[]{}<>«»´`¨&~=#±£¥$©®"
# must have spaces around them to make them tokens.
# Notes: they may be in pchar or fchar too, to identify punctuation after
#        a fchar.
#        \202 is a special ,
#        \226 \227 are special -
alonemarks = u"!?¿;,\202*¤@°:%|¦/()[\]{}<>«»´`¨&~=#±\226"+\
     u"\227£¥$©®\""
import unicodedata
for x in alonemarks:
    unicodename = unicodedata.name(x, '<unknown>')
    print "\t".join(map(unicode, (x, len(x), ord(x), unicodename, unicodedata.category(x))))

    # unichr(int('fd9b', 16)).encode('utf-8')
    # http://stackoverflow.com/questions/867866/convert-unicode-codepoint-to-utf8-hex-in-python    

我认为unicode数据库中的每个字符都被命名

不,控制字符没有名称,请参阅UnicodeData文件

另一个问题:如何获取 unicode 代码点值? 它是 ord(unicodechar) 吗?

是的!

print '%x' % ord(unicodedata.lookup('LATIN LETTER SMALL CAPITAL Z'))
## 1d22

根据unicodedata文档

该模块使用与 UnicodeData File Format 5.2.0 定义的相同的名称和符号(请参阅此处

您的两个字符显示以下输出:

1   150 <unknown>   Cc
1   151 <unknown>   Cc

它们对应于控制点字符 0x96 和 0x97 上面的 unicode 文档在代码点段落中规定:

代理代码点、专用字符、控制代码、非字符和未分配的代码点没有名称。

我不知道如何通过unicodedata模块获取与 unicode 注释相对应的标签注释,但我认为您没有为您的两个控制字符获得任何名称,因为它是由 Unicode 规范定义的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM