Python 中文转Unicode字符串

2023-01-31 01:01:24 python 中文 字符串

python 3.6

代码:

# -*- coding: utf-8 -*


def to_unicode(string):

    ret = ''
    for v in string:
        ret = ret + hex(ord(v)).upper().replace('0X', '\\u')

    return ret


print(to_unicode("中国"))

输出:

"D:\Program Files (x86)\python36-32\Python.exe" E:/PyCharmProjects/Test/test.py
\u4E2D\u56FD

Process finished with exit code 0

相关文章