Convering double backslash to single backslash in Python 3

后端 未结 3 1582
有刺的猬
有刺的猬 2021-01-05 23:13

I have a string like so:

>>> t
\'\\\\u0048\\\\u0065\\\\u006c\\\\u006c\\\\u006f\\\\u0020\\\\u20ac\\\\u0020\\\\u00b0\'

That I made u

3条回答
  •  失恋的感觉
    2021-01-05 23:32

    Not sure if this is appropriate for your situation, but you could try using unicode_escape:

    >>> t
    '\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0'
    >>> type(t)
    
    >>> enc_t = t.encode('utf_8')
    >>> enc_t
    b'\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0'
    >>> type(enc_t)
    
    >>> dec_t = enc_t.decode('unicode_escape')
    >>> type(dec_t)
    
    >>> dec_t
    'Hello € °'
    

    Or in abbreviated form:

    >>> t.encode('utf_8').decode('unicode_escape')
    'Hello € °'
    

    You take your string and encode it using UTF-8, and then decode it using unicode_escape.

提交回复
热议问题