Skip to content Skip to sidebar Skip to footer

Encoding On Postgresql, Python, Jinja2

I'm having a problem with encoding in my application and didn't find the solution anywhere on web. Here is the scenario: PostgreSQL with UTF-8 encoding (CREATE DATABASE xxxx WITH

Solution 1:

The issue is that psycopg2 returns byte strings by default in Python 2:

When reading data from the database, in Python 2 the strings returned are usually 8 bit str objects encoded in the database client encoding

So you can either:

  • Manually decode all of the data to UTF-8:

    # Decode the byte strings into Unicode objects using# the encoding you know that your database is using.
    companies = [company.decode("utf-8") for company in companies]
    return render_template("companies.html", companies=companies)
    

or

  • Set the encoders when you first import psycopg2 as per the note in the same section of the manual:

    Note In Python 2, if you want to uniformly receive all your database input in Unicode, you can register the related typecasters globally as soon as Psycopg is imported:

    import psycopg2
    import psycopg2.extensions
    psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
    psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
    

    and then forget about this story.

Post a Comment for "Encoding On Postgresql, Python, Jinja2"