Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'beginner': 0.05; 'insert': 0.05; 'postgresql': 0.07; 'skip:p 60': 0.07; 'assuming': 0.09; 'character,': 0.09; 'cursor': 0.09; 'exception,': 0.09; 'hallo,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'adapter': 0.16; 'codec': 0.16; 'first:': 0.16; 'goebel': 0.16; 'ordinal': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sqlite': 0.16; 'subject:values': 0.16; 'wrote:': 0.18; 'select': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; '2.x': 0.24; 'byte': 0.24; 'case.': 0.24; 'script.': 0.24; 'unicode': 0.24; 'looks': 0.24; 'suggested': 0.26; 'this:': 0.26; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'characters': 0.30; 'statement': 0.30; "i'm": 0.30; 'table': 0.34; 'subject:with': 0.35; "can't": 0.35; 'german': 0.35; 'prepare': 0.35; 'but': 0.35; 'url:org': 0.36; 'example,': 0.37; 'sometimes': 0.38; 'ends': 0.38; 'to:addr :python-list': 0.38; 'success.': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'solve': 0.60; 'course': 0.61; 'statement,': 0.68; 'special': 0.74; 'url:psycopg': 0.84; 'convinced': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Trouble with utf-8 values Date: Tue, 05 Nov 2013 10:00:45 +0100 Organization: None References: <5278496E.2000203@fam-goebel.de> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084b2d0.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383642063 news.xs4all.nl 15940 [2001:888:2000:d::a6]:41295 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:58486 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...