Python pyodbc Unicode issue
Question: I have a string variable res which I have derived from a pyodbc cursor as
shown in the bottom. The table `test` has a single row with data `ä` whose
unicode codepoint is `u'\xe4'`.

The Result I get is

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

Whereas the result I should have got is.

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

I tried adding charset as utf-8 to my pyodbc connect string as shown below.
The result was now correctly set as a unicode but the codepoint was for
someother string `꓃` which could be due to a possible bug in the 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 from test')
    res = cur.fetchall()[0][0]
    print(res)
    

Additional details Database: Teradata pyodbc version: 2.7

So How do I now either

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

2) resolve the pyodbc/unixodbc issue


Answer: This is something I think is best handled with Python, instead of fiddling
with [pyodbc.connect](https://code.google.com/p/pyodbc/wiki/Module) arguments
and driver-specific connection string attributes.

`'\xe4'` is a
[Latin-1](https://en.wikipedia.org/wiki/Latin-1_Supplement_%28Unicode_block%29)
encoded string representing the unicode
[ä](http://www.codetable.net/decimal/228) character.

To explicitly decode the 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 does this for you (the [`str` type includes unicode
characters](https://docs.python.org/3/howto/unicode.html#the-string-type)):

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

