Skip to content

Instantly share code, notes, and snippets.

@mrpollo
Created May 10, 2012 16:29
Show Gist options
  • Select an option

  • Save mrpollo/2654309 to your computer and use it in GitHub Desktop.

Select an option

Save mrpollo/2654309 to your computer and use it in GitHub Desktop.
python unicode from latin charmap UnicodeError
a = u'Teresa de Jes\xc3\u0192\xc2\xbas Galicia G\xc3\u0192\xc2\xb3mez'
b = u'EMMANUEL PE\xc3\u0192\xc2\u2018A GOMEZ PORTUGAL'
c = u'DR. JOS\xc3\u0192\xc2\u2030 ABEN\xc3\u0192\xc2\x81MAR RIC\xc3\u0192\xc2\x81RDEZ GARC\xc3\u0192\xc2\x8dA'
print a
print a.encode('cp1252').decode('utf-8').encode('cp1252').decode('utf-8')
print b
print b.encode('cp1252').decode('utf-8').encode('cp1252').decode('utf-8')
print c
print c.encode('cp1252').decode('utf-8').encode('cp1252').decode('utf-8')
Teresa de Jesús Galicia Gómez
Teresa de Jesús Galicia Gómez
EMMANUEL PEÑA GOMEZ PORTUGAL
Traceback (most recent call last):
File "regex.py", line 12, in <module>
print b.encode('cp1252').decode('utf-8').encode('cp1252').decode('utf-8')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/cp1252.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x91' in position 12: character maps to <undefined>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment