【问题标题】:Python3 - replacing non ascii characters to their unicode representative value? [duplicate]Python3 - 将非 ascii 字符替换为其 unicode 代表值? [复制]
【发布时间】:2021-12-18 19:34:13
【问题描述】:

假设我有一个字符串,
"Hello–World"

我如何将它转换成这样的东西
"Hello\u2013World"
其中"\u2013""–"的unicode代表

【问题讨论】:

    标签: python string unicode encoding


    【解决方案1】:

    str.encodeunicode_escape 一起使用:

    >>> print(s.encode('unicode_escape'))
    b'Hello\\u2013World'
    

    如果你想要一个字符串(和上面一样的字节字符串):

    >>> print(s.encode('unicode_escape').decode())
    Hello\u2013World
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 2016-04-06
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    相关资源
    最近更新 更多