当前位置: 首页>>代码示例>>Python>>正文


Python htmlentitydefs.codepoint2name方法代码示例

本文整理汇总了Python中htmlentitydefs.codepoint2name方法的典型用法代码示例。如果您正苦于以下问题:Python htmlentitydefs.codepoint2name方法的具体用法?Python htmlentitydefs.codepoint2name怎么用?Python htmlentitydefs.codepoint2name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在htmlentitydefs的用法示例。


在下文中一共展示了htmlentitydefs.codepoint2name方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_xmlcharnamereplace

# 需要导入模块: import htmlentitydefs [as 别名]
# 或者: from htmlentitydefs import codepoint2name [as 别名]
def test_xmlcharnamereplace(self):
        # This time use a named character entity for unencodable
        # characters, if one is available.

        def xmlcharnamereplace(exc):
            if not isinstance(exc, UnicodeEncodeError):
                raise TypeError("don't know how to handle %r" % exc)
            l = []
            for c in exc.object[exc.start:exc.end]:
                try:
                    l.append(u"&%s;" % htmlentitydefs.codepoint2name[ord(c)])
                except KeyError:
                    l.append(u"&#%d;" % ord(c))
            return (u"".join(l), exc.end)

        codecs.register_error(
            "test.xmlcharnamereplace", xmlcharnamereplace)

        sin = u"\xab\u211c\xbb = \u2329\u1234\u20ac\u232a"
        sout = "«ℜ» = ⟨ሴ€⟩"
        self.assertEqual(sin.encode("ascii", "test.xmlcharnamereplace"), sout)
        sout = "\xabℜ\xbb = ⟨ሴ€⟩"
        self.assertEqual(sin.encode("latin-1", "test.xmlcharnamereplace"), sout)
        sout = "\xabℜ\xbb = ⟨ሴ\xa4⟩"
        self.assertEqual(sin.encode("iso-8859-15", "test.xmlcharnamereplace"), sout) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:27,代码来源:test_codeccallbacks.py

示例2: test_customreplace_encode

# 需要导入模块: import htmlentitydefs [as 别名]
# 或者: from htmlentitydefs import codepoint2name [as 别名]
def test_customreplace_encode(self):
        if self.has_iso10646:
            self.skipTest('encoding contains full ISO 10646 map')

        from htmlentitydefs import codepoint2name

        def xmlcharnamereplace(exc):
            if not isinstance(exc, UnicodeEncodeError):
                raise TypeError("don't know how to handle %r" % exc)
            l = []
            for c in exc.object[exc.start:exc.end]:
                if ord(c) in codepoint2name:
                    l.append(u"&%s;" % codepoint2name[ord(c)])
                else:
                    l.append(u"&#%d;" % ord(c))
            return (u"".join(l), exc.end)

        codecs.register_error("test.xmlcharnamereplace", xmlcharnamereplace)

        if self.xmlcharnametest:
            sin, sout = self.xmlcharnametest
        else:
            sin = u"\xab\u211c\xbb = \u2329\u1234\u232a"
            sout = "«ℜ» = ⟨ሴ⟩"
        self.assertEqual(self.encode(sin,
                                    "test.xmlcharnamereplace")[0], sout) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:28,代码来源:multibytecodec_support.py

示例3: test_customreplace_encode

# 需要导入模块: import htmlentitydefs [as 别名]
# 或者: from htmlentitydefs import codepoint2name [as 别名]
def test_customreplace_encode(self):
        if self.has_iso10646:
            return

        from htmlentitydefs import codepoint2name

        def xmlcharnamereplace(exc):
            if not isinstance(exc, UnicodeEncodeError):
                raise TypeError("don't know how to handle %r" % exc)
            l = []
            for c in exc.object[exc.start:exc.end]:
                if ord(c) in codepoint2name:
                    l.append(u"&%s;" % codepoint2name[ord(c)])
                else:
                    l.append(u"&#%d;" % ord(c))
            return (u"".join(l), exc.end)

        codecs.register_error("test.xmlcharnamereplace", xmlcharnamereplace)

        if self.xmlcharnametest:
            sin, sout = self.xmlcharnametest
        else:
            sin = u"\xab\u211c\xbb = \u2329\u1234\u232a"
            sout = "«ℜ» = ⟨ሴ⟩"
        self.assertEqual(self.encode(sin,
                                    "test.xmlcharnamereplace")[0], sout) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:28,代码来源:test_multibytecodec_support.py


注:本文中的htmlentitydefs.codepoint2name方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。