Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python converting latin1 to UTF8 [duplicate]

In Python 2.7, how do you convert a latin1 string to UTF-8.

For example, I'm trying to convert é to utf-8.

>>> "é"
'\xe9'
>>> u"é"
u'\xe9'
>>> u"é".encode('utf-8')
'\xc3\xa9'
>>> print u"é".encode('utf-8')
é

The letter is é which is LATIN SMALL LETTER E WITH ACUTE (U+00E9) The UTF-8 byte encoding for is: c3a9
The latin byte encoding is: e9

How do I get the UTF-8 encoded version of a latin string? Could someone give an example of how to convert the é?

like image 570
Eugene Avatar asked Jan 21 '13 17:01

Eugene


2 Answers

To decode a byte sequence from latin 1 to Unicode, use the .decode() method:

>>> '\xe9'.decode('latin1')
u'\xe9'

Python uses \xab escapes for unicode codepoints below \u00ff.

>>> '\xe9'.decode('latin1') == u'\u00e9'
True

The above Latin-1 character can be encoded to UTF-8 as:

>>> '\xe9'.decode('latin1').encode('utf8')
'\xc3\xa9'
like image 137
Martijn Pieters Avatar answered Sep 19 '22 17:09

Martijn Pieters


>>> u"é".encode('utf-8')
'\xc3\xa9'

You've got a UTF-8 encoded byte sequence. Don't try to print encoded bytes directly. To print them you need to decode the encoded bytes back into a Unicode string.

>>> u"é".encode('utf-8').decode('utf-8')
u'\xe9'
>>> print u"é".encode('utf-8').decode('utf-8')
é

Notice that encoding and decoding are opposite operations which effectively cancel out. You end up with the original u"é" string back, although Python prints it as the equivalent u'\xe9'.

>>> u"é" == u'\xe9'
True
like image 30
John Kugelman Avatar answered Sep 18 '22 17:09

John Kugelman