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

后端 未结 5 941
生来不讨喜
生来不讨喜 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 04:05

    It took me a while to figure this one out, but this page had the best answer:

    >>> s = '\u003cfoo/\u003e'
    >>> s.decode( 'unicode-escape' )
    u''
    >>> s.decode( 'unicode-escape' ).encode( 'ascii' )
    ''
    

    There's also a 'raw-unicode-escape' codec to handle the other way to specify Unicode strings -- check the "Unicode Constructors" section of the linked page for more details (since I'm not that Unicode-saavy).

    EDIT: See also Python Standard Encodings.

提交回复
热议问题