From 66ab8aa2a4d53bbd4e6d867e2bcd47e7e45c36c4 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 20 Jul 2017 17:07:27 +0200 Subject: Imports: fix skip of the first line when import file is not utf-8 --- ishtar_common/models.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 5c84447a4..e49044cfd 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -2464,20 +2464,21 @@ class Import(models.Model): tmpdir = tempfile.mkdtemp(prefix='tmp-ishtar-') imported_file = z.extract(filename, tmpdir) - with open(imported_file) as csv_file: - encodings = [self.encoding] - encodings += [coding for coding, c in ENCODINGS] - for encoding in encodings: - try: + encodings = [self.encoding] + encodings += [coding for coding, c in ENCODINGS + if coding != self.encoding] + for encoding in encodings: + try: + with open(imported_file) as csv_file: vals = [line for line in unicodecsv.reader(csv_file, encoding=encoding)] if tmpdir: shutil.rmtree(tmpdir) + print(encoding, imported_file, vals[0]) return vals - except UnicodeDecodeError: - if encoding != encodings[-1]: - csv_file.seek(0) + except UnicodeDecodeError: + print(encoding, encodings) if tmpdir: shutil.rmtree(tmpdir) return [] -- cgit v1.2.3