How to trap this error (comes from sqlite3)

cl at isbd.net cl at isbd.net
Tue Feb 9 09:31:25 EST 2016


Peter Otten <__peter__ at web.de> wrote:
> cl at isbd.net wrote:
> 
> > It's absolutely right, there is a non-UTF character in the column.
> > However I don't want to have to clean up the data, it would take
> > rather a long time.  How can I trap the error and just put a Null or
> > zero in the datagrid?
> > 
> > Where do I put the try:/except: ?
> 
> Handle the problem earlier on by setting a text_factory that ignores or 
> replaces the offending bytes:
> 
> db = sqlite3.connect(...)
> db.text_factory = lambda b: b.decode("utf-8", "replace")
> 
> https://docs.python.org/2.7/library/sqlite3.html#sqlite3.Connection.text_factory
> https://docs.python.org/2/howto/unicode.html
> 
Brilliant, thank you, works perfectly and (IMHO) is about the neatest
possible solution.

-- 
Chris Green
·


More information about the Python-list mailing list