From f02e886976d887d6afbe3669104a81066228134d Mon Sep 17 00:00:00 2001 From: Samuel Findlay Date: Sat, 9 Jun 2012 04:30:34 +1000 Subject: [PATCH] Clean import strings of ASCII control chars (user's db had x07 chars) --- openlp/plugins/songs/lib/zionworximport.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/openlp/plugins/songs/lib/zionworximport.py b/openlp/plugins/songs/lib/zionworximport.py index bf68cdf3d..8f83c4509 100644 --- a/openlp/plugins/songs/lib/zionworximport.py +++ b/openlp/plugins/songs/lib/zionworximport.py @@ -77,6 +77,9 @@ class ZionWorxImport(SongImport): """ Receive a CSV file (from a ZionWorx database dump) to import. """ + # Used to strip control chars (10=LF, 13=CR, 127=DEL) + self.control_chars_map = dict.fromkeys( + range(10) + [11, 12] + range(14,32) + [127]) with open(self.importSource, 'rb') as songs_file: fieldnames = [u'SongNum', u'Title1', u'Title2', u'Lyrics', u'Writer', u'Copyright', u'Keywords', u'DefaultStyle'] @@ -131,9 +134,9 @@ class ZionWorxImport(SongImport): def _decode(self, str): """ - Decodes CSV input to unicode. - - This encoding choice seems OK. ZionWorx has no option for setting the - encoding for its songs, so we assume encoding is always the same. + Decodes CSV input to unicode, stripping all control characters (except + new lines). """ - return unicode(str, u'cp1252') + # This encoding choice seems OK. ZionWorx has no option for setting the + # encoding for its songs, so we assume encoding is always the same. + return unicode(str, u'cp1252').translate(self.control_chars_map)