How do I treat an ASCII string as unicode and unescape the escaped characters in it in python?

后端 未结 5 936
生来不讨喜
生来不讨喜 2020-11-30 03:13

For example, if I have a unicode string, I can encode it as an ASCII string like so:

>>> u\'\\u003cfoo/\\u003e\'.encode(\'ascii\')         


        
5条回答
  •  清歌不尽
    2020-11-30 03:47

    On Python 2.5 the correct encoding is "unicode_escape", not "unicode-escape" (note the underscore).

    I'm not sure if the newer version of Python changed the unicode name, but here only worked with the underscore.

    Anyway, this is it.

提交回复
热议问题