python如何实现字符串与'\u'开头的unicode字符串相互转换

字符串转unicode字符串技术要点:

  • ord()函数

  • format()函数

代码:

def str_to_unicode(string, upper=True):
    '''字符串转unicode'''
    if upper is True:
        return ''.join(rf'\u{ord(x):04X}' for x in string)
    else:
        return ''.join(rf'\u{ord(x):04x}' for x in string)

def unicode_to_str(unicode):
    '''unicode转字符串'''
    if isinstance(unicode, bytes):
        return unicode.decode('unicode_escape')
    else:
        return unicode.encode('utf-8').decode('unicode_escape')

if __name__ == '__main__':
    text = 'hello中国'
    lower_uni = str_to_unicode(text, upper=False)
    upper_uni = str_to_unicode(text)

    print(f'{text}  =>  {lower_uni}')
    print(f'{text}  =>  {upper_uni}')
    
    print(f'{lower_uni}  =>  {unicode_to_str(lower_uni)}')
    print(f'{upper_uni}  =>  {unicode_to_str(upper_uni)}')

执行结果:

hello中国  =>  \u0068\u0065\u006c\u006c\u006f\u4e2d\u56fd
hello中国  =>  \u0068\u0065\u006C\u006C\u006F\u4E2D\u56FD
\u0068\u0065\u006c\u006c\u006f\u4e2d\u56fd  =>  hello中国
\u0068\u0065\u006C\u006C\u006F\u4E2D\u56FD  =>  hello中国

 

posted @ 2023-01-14 04:30  eliwang  阅读(0)  评论(0编辑  收藏  举报