首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python、unicodedata名称和codepoint值,我错过了什么?

Python、unicodedata名称和codepoint值,我错过了什么?
EN

Stack Overflow用户
提问于 2012-04-27 17:47:47
回答 2查看 1.1K关注 0票数 5

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

我以为unicode数据库里的每一个字符都被命名了,顺便说一句,它们都属于同一个类别,那就是[Cc] Other, Control

另一个问题:如何获取unicode代码点值?是不是ord(unicodechar)做到了呢?

我还把文件的here (因为编码是一个奇怪的东西),因为我认为我的剪切n‘粘贴与’不可见‘字符可能是有损的。

代码语言:javascript
复制
#!/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    
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-27 18:09:55

我以为unicode数据库里的每一个字符都被命名为

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

另一个问题:如何获得unicode代码点值?是ord(unicodechar)起作用了吗?

是!

代码语言:javascript
复制
print '%x' % ord(unicodedata.lookup('LATIN LETTER SMALL CAPITAL Z'))
## 1d22
票数 3
EN

Stack Overflow用户

发布于 2012-04-27 18:24:41

根据unicodedatadocumentation

该模块使用的名称和符号与UnicodeData文件格式5.2.0定义的名称和符号相同(请参见here)

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

代码语言:javascript
复制
1   150 <unknown>   Cc
1   151 <unknown>   Cc

它们对应于控制点字符0x96和0x97上面的unicode文档在the code point paragraph中规定:

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10348434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档