decode(unicode_escape) in python 3 a string

后端 未结 1 1204
鱼传尺愫
鱼传尺愫 2020-12-18 08:19

I\'ve checked this solution but it doesn\'t work in python3.

I\'ve an escaped string of this kind: str = \"Hello\\\\nWorld\" and I want to obtain the sa

相关标签:
1条回答
  • 2020-12-18 08:33

    decode applies to bytes, which you can create by encoding from your string.

    I would encode (using default) then decode with unicode-escape

    >>> s = "Hello\\nWorld"
    >>> s.encode()
    b'Hello\\nWorld'
    >>> s.encode().decode("unicode-escape")
    'Hello\nWorld'
    >>> print(s.encode().decode("unicode-escape"))
    Hello
    World
    >>> 
    
    0 讨论(0)
提交回复
热议问题