Python psycopg2 not in utf-8

Fedour picture Fedour · Apr 24, 2017 · Viewed 10.8k times · Source

I use Python to connect to my postgresql data base like this:

conn=psycopg2.connect(database="fedour", user="fedpur", password="***", host="127.0.0.1", port="5432")

No problem for that.

But when I make a query and I want to print the cursor I have something like this:

"Fran\xc3\xa7ois" instead of "François" and it cause problem when I want to create a XML document with this. I thkink is come from my encodage, but I found any solution. I try to encode('utf-8') but doesn't work.

I have also seen something like this, but its only for mySQL

MySQLdb.connect(charset='utf8', init_command='SET NAMES UTF8')

Can you help me ? Thanks

Answer

Leonardo Chirivì picture Leonardo Chirivì · Apr 24, 2017

Make sure you're using the right encodind by running: print conn.encoding, and if you need, you can set the right encoding by conn.set_client_encoding('UNICODE'), or conn.set_client_encoding('UTF8').