What's the best/neatest way to get Unicode data from a database into a grid cell?

Dietmar Schwertberger maillist at schwertberger.de
Sun Feb 7 10:06:24 EST 2016


On 07.02.2016 12:19, cl at isbd.net wrote:
> However my database has quite a lot of Unicode data as there are
> French (and other) names with accents etc.  What's the right way to
> handle this reasonably neatly?  At the moment it traps an error at
> line 37:-
>      self.SetCellValue(row_num, i, str(cells[i]))
The unicode versions of wxPython should have no problems with handling 
unicode strings.
The problem that you see here is that str(...) tries to convert your 
unicode data into a non-unicode string, which of course fails.
Do something like:

value = cells[i]
if not isinstance(value, basestring):
     value = str(value)
self.SetCellValue(row_num, i, value)

Once you switch to Python 3 and Phoenix you have to modify this 
slightly, e.g. by adding this to the top of your code:

try:
     basestring
except:
     basestring = (bytes,str)


Regards,

Dietmar



More information about the Python-list mailing list