开发者

Python: print unicode char escaped

I have tried to convert an ascii string to an escaped pseudo unicode escaped string using python, but failed so far.

What I want to do: Convert ASCII 'a' to ASCII String "<U0061>"

I can convert "a" with unicode('a'), but can not safe the numerical valu开发者_JAVA技巧e of a in an ascii string.

How can I do that?


You can use ord() to convert a character to its character value (str) or code point (unicode). You can then use the appropriate string formatting to convert it into a text representation.

'U+%04X' % (ord(u'A'),)


Here goes a minimalist sample that allows you to use Ignacio's solution with Python's built-in coding/decoding engine. Check http://docs.python.org/library/codecs.html if you need something more consistent (with proper error handling, etc...)

import codecs


def  encode(text, error="strict"):
    return ("".join("<U%04x>" % ord(char) for char in text), len(text))

def search(name):
    if name == "unicode_ltgt":
        info = codecs.CodecInfo(encode, None, None, None)
        info.name = "unicode_ltgt"
        info.encode = encode
        return info
    return None

codecs.register(search)

if __name__ == "__main__":
    a = u"maçã"
    print a.encode("unicode_ltgt")

(just by importing this as a module, the codec "unicode_ltgt" will be installed and be available to any ".encode" call, like in the given example )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜