一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

python将字符实体引用转换成 Unicode 字符

时间:2016-08-06 编辑:简简单单 来源:一聚教程网


HTML Entities 的格式如:<,NCR 的格式如:< 或 <,均都表示“<” 字符。
HTML 中规定了 Character entity references,在 “24.2.1 The list of characters” 列出了 HTML Entities 和 NCR 的对应关系,例如:



那在 Python 中我们如何将 HTML Entities 和 NCR 转换成普通字符呢?

在回答这个问题之前,我们做一些简单的回顾:

group 方法

group([group1,…])
group 属于 Match Object 对象拥有的方法,返回匹配到的一个或者多个子组。如果是一个参数,那么结果返回字符串,如果是多个参数,则返回元组。group1 的默认值为 0 (将返回所有的匹配值),如果 groupX 的值是 [1…99] 范围之内的,那么将匹配对应括号组的字符串。如果组号是负的或者比 pattern 中定义的组号大,那么将抛出 IndexError 异常。若 pattern 没有匹配到,但 group 匹配到,那么 group 的值也为 None。如果一个 pattern 可以匹配多个,那么组对应匹配的最后一个。
re.sub 方法
re.sub(pattern , replace , string [, count])
sub 属于 re 模块的字符串替换和修改函数,其在目标字符串中查找与正则相匹配的字符串,并将其替换成指定的字符串。
pattern 参数——需要匹配的正则规则
replace 参数——指定用来替换的字符串或函数。如果 replace 是函数,则会对所有的匹配都回调此函数,这个函数使用单个 Match Object 作为参数,然后返回替换后的字符串。
string 参数——目标字符串
count 参数——最多替换的次数,未指定,则将替换所有匹配到的字符串
re.sub() 的使用案例如下:

 代码如下 复制代码

import re
def dashrepl(matchobj):
    if matchobj.group(0) == '-':
        return ' '
    else:
        return '-'
re.sub('-{1,2}', dashrepl, 'pro----gram-files')

# result: 'pro--gram files'
htmlentitydefs
htmlentitydefs 有三个属性,详细如下:
entitydefs:A dictionary mapping XHTML 1.0 entity definitions to their replacement text in ISO Latin-1.
name2codepoint:A dictionary that maps HTML entity names to the Unicode codepoints. New in version 2.3.
codepoint2name:A dictionary that maps Unicode codepoints to HTML entity names.

实际存在的形式大致如下:

 代码如下 复制代码

entitydefs = {'AElig': '\xc6', 'Aacute': '\xc1', 'Acirc': '\xc2', ...}
name2codepoint = {'AElig': 198, 'Aacute': 193, 'Acirc': 194, ...}
codepoint2name = {34: 'quot', 38: 'amp', 60: 'lt', 62: 'gt', ...}

对于我们来说,此时最有用的是 name2codepoint 属性,比如:“<”,name 是 lt,我们可以通过 name2codepoint[lt] 获得其 code point:60。

unichr 方法

unichr 是字符串的方法(unichr(int)),可以将整数转化成相应的 Unicode 字符,比如: unichr(60) –> u'\u003c' or u'<'

 代码如下 复制代码

import re, htmlentitydefs

##
# Removes HTML or XML character references and entities from a text string.
#
# @param text The HTML (or XML) source text.
# @return The plain text, as a Unicode string, if necessary.

def unescape(text):
    def convert(matchobj):
        text = matchobj.group(0)
        if text[:2] == "&#":
            # Numeric Character Reference
            try:
                if text[:3] == "&#x":
                    return unichr(int(text[3:-1], 16))
                else:
                    return unichr(int(text[2:-1]))
            except ValueError:
                pass
        else:
            # Character entities references
            try:
                text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
            except KeyError:
                pass
        return text # Return Unicode characters
    return re.sub("&#?\w+;", convert, text)

网友提供了将Unicode中文字符串转换成 string字符串

普通字符串可以用多种方式编码成Unicode字符串,具体要看你究竟选择了哪种编码:

 代码如下 复制代码
unicodestring = u"Hello world"
# 将Unicode转化为普通Python字符串:"encode" 
utf8string = unicodestring.encode("utf-8") 
asciistring = unicodestring.encode("ascii") 
isostring = unicodestring.encode("ISO-8859-1") 
utf16string = unicodestring.encode("utf-16") 
# 将普通Python字符串转化为Unicode:"decode" 
plainstring1 = unicode(utf8string, "utf-8") 
plainstring2 = unicode(asciistring, "ascii") 
plainstring3 = unicode(isostring, "ISO-8859-1") 
plainstring4 = unicode(utf16string, "utf-16") 
assert plainstring1 == plainstring2 == plainstring3 == plainstring4

热门栏目