Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python psycopg2 not in utf-8

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

like image 783
Fedour Avatar asked Apr 24 '17 08:04

Fedour


People also ask

Should I use psycopg2 binary?

The psycopg2-binary package is meant for beginners to start playing with Python and PostgreSQL without the need to meet the build requirements. If you are the maintainer of a published package depending on psycopg2 you shouldn't use psycopg2-binary as a module dependency.

Is psycopg2 asynchronous?

Psycopg allows asynchronous interaction with other database sessions using the facilities offered by PostgreSQL commands LISTEN and NOTIFY.

What is Python3 psycopg2?

Python 3 module for PostgreSQL psycopg is a PostgreSQL database adapter for the Python3 programming language (just like pygresql and popy.) This is version 2, a complete rewrite of the original code to provide new-style classes for connection and cursor objects and other sweet candies.


1 Answers

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').

like image 165
lch Avatar answered Oct 13 '22 17:10

lch