简体   繁体   中英

How can I get python ''.encode('unicode_escape') to return escape codes for ascii?

["

I am trying to use the encode<\/code> method of python strings to return the unicode escape codes for characters, like this:<\/i>

>>> print( 'ф'.encode('unicode_escape').decode('utf8') )
\u0444

ord can be used for this, there is no need for encoding/decoding at all:

>>> '"\\U{:08x}"'.format(ord('f'))  # ...or \u{:04x} if you prefer
'"\\U00000066"'
>>> eval(_)
'f'
["

You'd have to do so manually;<\/i>

import re

def unicode_escaped(s, _pattern=re.compile(r'[\x00-\uffff]')):
    return _pattern.sub(lambda m: '\\u{:04x}'.format(
        ord(m.group(0))), s)

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