简体   繁体   中英

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 value 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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