UnicodeEncodeError: 'latin-1' codec can't encode character

Viewed 276200

What could be causing this error when I try to insert a foreign character into the database?

>>UnicodeEncodeError: 'latin-1' codec can't encode character u'\u201c' in position 0: ordinal not in range(256)

And how do I resolve it?

Thanks!

11 Answers

Use the below snippet to convert the text from Latin to English

import unicodedata
def strip_accents(text):
    return "".join(char for char in
                   unicodedata.normalize('NFKD', text)
                   if unicodedata.category(char) != 'Mn')

strip_accents('áéíñóúü')

output:

'aeinouu'

UnicodeEncodeError: 'latin-1' codec can't encode character '\u2013' in position 106: ordinal not in range(256)

Solution 1: \u2013 - google the character meaning to identify what character actually causing this error, Then you can replace that specific character, in the string with some other character, that's part of the encoding you are using.

Solution 2: Change the string encoding to some encoding which includes all the character of your string. and then you can print that string, it will work just fine.

below code is used to change encoding of the string , borrowed from @bobince

 u'He said \u201CHello\u201D'.encode('cp1252')

The latest version of mysql.connector has only

db.set_charset_collation('utf8', 'utf8_general_ci')

and NOT

db.set_character_set('utf8') //This feature is not available
Related