Character Encoding Question

Поиск
Список
Период
Сортировка
От Don Parris
Тема Character Encoding Question
Дата
Msg-id CAJ-7yonFRiBcaYVcccfF0zHjhNToK2aWrAVaeLW6Kj4Gk_tvcw@mail.gmail.com
обсуждение исходный текст
Ответы Re: Character Encoding Question  (Daniele Varrazzo <daniele.varrazzo@gmail.com>)
Список 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
GPG Key ID: F5E179BE

В списке psycopg по дате отправления:

Предыдущее
От: Karsten Hilbert
Дата:
Сообщение: Re: Dropping Python 2.4 support
Следующее
От: Daniele Varrazzo
Дата:
Сообщение: Re: Character Encoding Question