Re: Trouble with utf-8 values

2013-11-05 Thread Peter Otten
Ulrich Goebel wrote:

 Hallo,
 
 again: a python beginner problem... but I spent ours to solve it without
 success.
 
 I have an object (a variable) name, which gets its value from a
 PostgreSQL database via a SELECT statement, an it sometimes has german
 special characters as ß, ä, ö...
 
 Then I would like to insert that value into a table in a SQLite
 database. So I make a cursor cur on the table and prepare a SQL
 statement like this:
 
 sql = 'insert into tbl values(?)'
 cur.execute(sql, (name,))
 
 That ends up with the exception, for example,
 
 UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6:
 ordinal not in range(128)
 
 The position 6 is exactly the position of the special character, ß in
 this case.
 
 What to do?

While sqlite works with unicode out of the box it looks like the PostgreSQL 
adapter needs to be convinced first:

http://initd.org/psycopg/docs/usage.html#unicode-handling

Try adding the voodoo suggested above

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

to your script. I'm of course assuming you are using python 2.x and 
pyscopg2...



-- 
https://mail.python.org/mailman/listinfo/python-list


Trouble with utf-8 values

2013-11-04 Thread Ulrich Goebel

Hallo,

again: a python beginner problem... but I spent ours to solve it without 
success.


I have an object (a variable) name, which gets its value from a 
PostgreSQL database via a SELECT statement, an it sometimes has german 
special characters as ß, ä, ö...


Then I would like to insert that value into a table in a SQLite 
database. So I make a cursor cur on the table and prepare a SQL 
statement like this:


sql = 'insert into tbl values(?)'
cur.execute(sql, (name,))

That ends up with the exception, for example,

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: 
ordinal not in range(128)


The position 6 is exactly the position of the special character, ß in 
this case.


What to do?

Ulrich


--
Ulrich Goebel
Paracelsusstr. 120, 53177 Bonn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Trouble with utf-8 values

2013-11-04 Thread Ben Finney
Ulrich Goebel m...@fam-goebel.de writes:

 I have an object (a variable) name

This confuses me. Is it an object, a variable, or a name?

 which gets its value from a PostgreSQL database via a SELECT
 statement, an it sometimes has german special characters as ß, ä, ö...

What is the type of that object? You can interrogate Python for the
answer, by using ‘type(foo)’ which will return the type of the object
named by “foo”.

 Then I would like to insert that value into a table in a SQLite
 database. So I make a cursor cur on the table and prepare a SQL
 statement like this:

 sql = 'insert into tbl values(?)'
 cur.execute(sql, (name,))

What version of Python are you running?

Do you get different results in Python 2 versus Python 3? One of the
biggest improvements in Python 3 is the proper handling of Unicode, with
more explicit rejection of ambiguity between bytes versus text.

-- 
 \  “The face of a child can say it all, especially the mouth part |
  `\of the face.” —Jack Handey |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list