Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting u"string" to "string" in Python without changing encoding

Tags:

python

unicode

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. My main goal is to be able to get the following result, so any shortcut to get there would also be accepted:

>>> '\x96'.decode("cp1252")
u'\u2013'

In other words, I have u'\x96' and I want u'\u2013'. Any help would be appreciated.

I'm using Python 2.7.

like image 804
martin Avatar asked Aug 10 '11 09:08

martin


1 Answers

u'\x96'.encode('raw_unicode_escape').decode("cp1252")
like image 104
mouad Avatar answered Oct 18 '22 01:10

mouad