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

Vlastimil Brom vlastimil.brom at gmail.com
Sun Feb 7 13:22:34 EST 2016


2016-02-07 12:19 GMT+01:00  <cl at isbd.net>:
> I'm using this as a starting point for creating a grid to view and
> edit some sqlite data:-
>     http://www.salstat.com/news/linking-wxgrid-to-sqlite-database-in-python-an-example
>
> I can actually understand most of it which is a good start.
>
> 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]))
>
> I realise why this fails (I think) but how should one program this so
> that:-
>     1 - the accented characters are displayed correctly in the grid cell
>     2 - One can edit the cell with accented characters
>
> (I'll get round how to get the accented characters back to the
> database later!)
>
> My system (xubuntu 15.10) is all UTF8 so accented characters are
> handled by the display, in terminals, etc. correctly.  I'm currently
> using python 2.7 for this but would be quite happy to move to 3.4 if
> this handles UTF8 better (I seem to remember it does maybe).
>
> --
> Chris Green
> ·
> --
> https://mail.python.org/mailman/listinfo/python-list

Hi,
your code in
http://www.salstat.com/news/linking-wxgrid-to-sqlite-database-in-python-an-example

seems to work for me after small changes with both python 2.7 and 3.4
(using wx Phoenix)
the changes I made are:
- encoding declaration at the beginning of the file (mainly for py 2,
if utf-8 is used):

#! Python
# -*- coding: utf-8 -*-

- removing the str(...) call in the offending line you mentioned:
 self.SetCellValue(row_num, i, cells[i])
(what was the intent of the conversion to str?)

- corrected event name (insted of EVT_GRID_CELL_CHANGE)
self.Bind(gridlib.EVT_GRID_CELL_CHANGED, self.CellContentsChanged)

- normal App() call (instead of the PySimpleApp)

app = wx.App()

I randomly tried some characters using Latin, Greek etc. characters
(within BMP) as well as Gothic - beyond the FFFF range - the cell
content seems to be saved and and newly loaded from the sqlite db on
subsequent calls of the app.

regards,
    vbr



More information about the Python-list mailing list