Character Encoding Question

From: Don Parris <parrisdc(at)gmail(dot)com>
To: psycopg(at)postgresql(dot)org
Subject: Character Encoding Question
Date: 2013-03-28 15:35:45
Message-ID: CAJ-7yonFRiBcaYVcccfF0zHjhNToK2aWrAVaeLW6Kj4Gk_tvcw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: psycopg

Hi all,

I am using Psycopg2 with Python3 & PostgreSQL 9.1 with a database encoded
as UTF-8 (Kubuntu 12.10). I have a question about the encoding and
decoding, and why some fetch calls did what I wanted while fetchall()
seemed to choke. I got the following error message when calling fetchall():
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 9:
ordinal not in range(128)

The traceback pointed to the fetchall() statement.

First, I was able to do a pattern matching search on a specific field in my
payee table and could get back whatever records matched the user input in
my script. Then I ran a simple SELECT on the same table, using fetchall(),
but that choked.

I decided to experiment some before asking questions here on the list, and
discovered that, if I call fetchone(), I got a single record with the
fields presented as rows in my Bash console. Then I tried fetchmany() and
got the same record presented as a tuple.

In the psycopg usage doc (
http://www.initd.org/psycopg/docs/usage.html#unicode-handling) I found
this reference:

conn.set_client_encoding('LATIN9')

I tried that, but set the encoding to UTF-8 instead of the LATIN9,
supposing that might be useful. Given the successful result, it appears
that was just what the doctor ordered!

My first question is, is this really all I need to do - just set the client
encoding? Or is there something else I need to do or be aware of?

My second question is why does fetchall() fail without setting the client
encoding (while the other fetch calls work)?

Thanks!
Don
--
D.C. Parris, FMP, Linux+, ESL Certificate
Minister, Security/FM Coordinator, Free Software Advocate
http://dcparris.net/
<https://www.xing.com/profile/Don_Parris><http://www.linkedin.com/in/dcparris>
GPG Key ID: F5E179BE

Responses

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2013-03-28 15:49:04 Re: Character Encoding Question
Previous Message Karsten Hilbert 2013-03-20 11:37:24 Re: Dropping Python 2.4 support