[Tutor] Handling a Unicode Return using Pyodbc

Marc Tompkins marc.tompkins at gmail.com
Mon Nov 14 23:05:51 CET 2011


On Mon, Nov 14, 2011 at 1:43 PM, Tony Pelletier <tony.pelletier at gmail.com>wrote:

> Good Afternoon,
>
> I'm writing a program that is essentially connecting to MS SQL Server and
> dumping all the contents of the tables to separate csv's.  I'm almost
> complete, but now I'm running into a Unicode issue and I'm not sure how to
> resolve it.
>
> I have a ridiculous amount of tables but I managed to figure out it was my
> Contact and a contact named Robert Bock.  Here's what I caught.
>
> (127, None, u'Robert', None, u'B\xf6ck', 'uCompany Name', None, 1, 0, 327,
> 0)
>
> The u'B\xf6ck' is actually Böck.  Notice the ö
>
> My problem is I'm not really sure how to handle it and whether or not it's
> failing on the query or the insert to the csv.  The Exception is:
>
> 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in
> range(128)
>
>
We'd be able to help a lot better if you post the _entire_ error
message/traceback; for one thing, the traceback ought to give enough info
to determine which operation is throwing the error (query or insert).

That being said, when you do figure out where it's happening, the solution
will be change how you're handling the string: you'll need to explicitly
run it through the .encode() method, using either a Unicode-aware codec OR
the default ASCII codec with errors="replace" or errors="ignore" (my vote
would be for Unicode, of course, but it's your data.)

Here's a page you might find useful:
http://docs.python.org/howto/unicode.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111114/9eaf13d0/attachment-0001.html>


More information about the Tutor mailing list