Python pyodbc Unicode issue -



Python pyodbc Unicode issue -

i have string variable res have derived pyodbc cursor shown in bottom. table test has single row info ä unicode codepoint u'\xe4'.

the result

>>> res,type(res) ('\xe4', <type 'str'>)

whereas result should have got is.

>>> res,type(res) (u'\xe4', <type 'unicode'>)

i tried adding charset utf-8 pyodbc connect string shown below. result correctly set unicode codepoint someother string due possible bug in pyodbc driver.

conn = pyodbc.connect(dsn='datbase;charset=utf8',ansi=true,autocommit=true) >>> res,type(res) (u'\ua4c3', <type 'unicode'>)

actual code

import pyodbc pyodbc.pooling=false conn = pyodbc.connect(dsn='datbase',ansi=true,autocommit=true) cursor = conn.cursor() cur = cursor.execute('select col1 test') res = cur.fetchall()[0][0] print(res)

additional details database: teradata pyodbc version: 2.7

so how either

1) cast ('\xe4', <type 'str'>) (u'\xe4', <type 'unicode'>) (is possible without unintentional side-effects?)

2) resolve pyodbc/unixodbc issue

this think best handled python, instead of fiddling pyodbc.connect arguments , driver-specific connection string attributes.

'\xe4' latin-1 encoded string representing unicode ä character.

to explicitly decode pyodbc result in python 2.7:

>>> res = '\xe4' >>> res.decode('latin1'), type(res.decode('latin1')) (u'\xe4', <type 'unicode'>) >>> print res.decode('latin1') ä

python 3.x (the str type includes unicode characters):

>>> res = '\xe4' >>> res, type(res) ('ä', <class 'str'>) >>> print(res) ä

python unicode utf-8 odbc pyodbc

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -