How to convert some character into five digit unicode one in Python 3.3?

后端 未结 2 1350
别跟我提以往
别跟我提以往 2020-12-20 21:00

I\'d like to convert some character into five digit unicode on in Python 3.3. For example,

import re
print(re.sub(\'a\', u\'\\u1D15D\', \'abc\' ))

相关标签:
2条回答
  • 2020-12-20 21:39

    By the way, you do not need the re module for this. You could use str.translate:

    >>> 'abc'.translate({ord('a'):'\U0001D15D'})
    '                                                                    
    0 讨论(0)
  • 2020-12-20 21:51

    Python unicode escapes either are 4 hex digits (\uabcd) or 8 (\Uabcdabcd); for a codepoint beyond U+FFFF you need to use the latter (a capital U), make sure to left-fill with enough zeros:

    >>> '\U0001D15D'
    '                                                                    
    0 讨论(0)
提交回复
热议问题