[Fixed]-UnicodeEncodeError:'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)

39👍

I’m assuming that you’re using MySQL with the MySQLdb driver here.

The default encoding used by the MySQLdb driver is latin-1, which does not support your character set. You’ll need to use UTF-8 (or others, but UTF-8 is the most common) to be able to communicate with your database through MySQLdb (see http://docs.sqlalchemy.org/en/rel_0_8/dialects/mysql.html#unicode).

To do such a thing, create your engine with the following line:

create_engine('mysql+mysqldb://USER:@SERVER:PORT/DB?charset=utf8', encoding='utf-8')

You can also construct your engine url using the sqlalchemy.engine.url.URL class, and send it to the create engine function. I find it useful when you have your settings in a config file.

import sqlalchemy.engine.url as url

engine_url = url.URL(
    drivername='mysql+' + cfg['MYSQL_PYTHON_DRIVER'],
    host=cfg['MYSQL_HOST'],
    port=cfg['MYSQL_PORT'],
    username=cfg['MYSQL_USER'],
    password=cfg['MYSQL_PWD'],
    database=cfg['MYSQL_DB'],
    query={'charset': 'utf8'}
)
db = create_engine(engine_url, encoding='utf-8')

Hope that helps.

4👍

based on your stacktrace, you’re using MySQL Python with unicode encoding turned on, since it’s doing an encode. So you likely need to specify a comaptible encoding (note this is all settings used by the MySQLdb DBAPI, SQLalhcemy just passes them through):

create_engine('mysql+mysqldb:///mydb?charset=utf8&use_unicode=1')

http://docs.sqlalchemy.org/en/rel_0_8/dialects/mysql.html#unicode

👤zzzeek

Leave a comment