From 4c7379a898df426af0c31159e5cb06d672189301 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 24 Oct 2017 17:01:34 -0700 Subject: [PATCH] Don't crash on weird character encodings Expecting SQLite columns to all be valid utf8 doesn't work, because we are deailing with all kinds of databases. Instead, we now use the 'replace' encoding mode to replace any non-UTF8 characters with a [X] character. --- app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 8ebbc66b..dc0711c1 100644 --- a/app.py +++ b/app.py @@ -34,6 +34,7 @@ def get_conn(name): uri=True ) conns[name].row_factory = sqlite3.Row + conns[name].text_factory = lambda x: str(x, 'utf-8', 'replace') return conns[name] @@ -279,7 +280,7 @@ def pks_for_table(conn, table): if row[-1] ] rows.sort(key=lambda row: row[-1]) - return [r[1] for r in rows] + return [str(r[1]) for r in rows] def path_from_row_pks(row, pks):