Skip to content Skip to sidebar Skip to footer

Converting U"string" To "string" In Python Without Changing Encoding

I have the following: u'\x96' I want to convert it to the following: '\x96' Is there any way to do this? str() doesn't work, and when using .encode(...) it changes the encoding.

Solution 1:

u'\x96'.encode('raw_unicode_escape').decode("cp1252")

Solution 2:

Latin-1 is the encoding that directly maps the first 256 characters of Unicode to their byte values.

>>> u'\x96'.encode('latin-1').decode("cp1252")
u'\u2013'

Post a Comment for "Converting U"string" To "string" In Python Without Changing Encoding"