summaryrefslogtreecommitdiff
path: root/ishtar_common/models_imports.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2019-03-27 16:17:07 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2019-04-24 19:41:37 +0200
commitd78001a6f707a27d5a1643179243a222057d0bda (patch)
treeec59521a9c454faef7bf4af91dc13ca4938596fd /ishtar_common/models_imports.py
parent33171a9b1ba785ae1ff68934509c967b1a8e4348 (diff)
downloadIshtar-d78001a6f707a27d5a1643179243a222057d0bda.tar.bz2
Ishtar-d78001a6f707a27d5a1643179243a222057d0bda.zip
CSV import: manage semi-colon as separator
Diffstat (limited to 'ishtar_common/models_imports.py')
-rw-r--r--ishtar_common/models_imports.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py
index 380a582cf..a774cef6d 100644
--- a/ishtar_common/models_imports.py
+++ b/ishtar_common/models_imports.py
@@ -807,6 +807,9 @@ ENCODINGS = [(settings.ENCODING, settings.ENCODING),
(settings.ALT_ENCODING, settings.ALT_ENCODING),
('utf-8', 'utf-8')]
+CSV_SEPS = ((u",", u","),
+ (u";", u";"),)
+
delayed_import = None
delayed_check = None
@@ -848,6 +851,11 @@ class Import(models.Model):
)
encoding = models.CharField(_(u"Encoding"), choices=ENCODINGS,
default=u'utf-8', max_length=15)
+ csv_sep = models.CharField(
+ _(u"CSV separator"), choices=CSV_SEPS, default=u',', max_length=1,
+ help_text=_(u"Separator for CSV file. Standard is comma but Microsoft "
+ u"Excel do not follow this standard and use semi-colon.")
+ )
skip_lines = models.IntegerField(
_(u"Skip lines"), default=1,
help_text=_(u"Number of header lines in your file (can be 0)."))
@@ -919,7 +927,7 @@ class Import(models.Model):
filename = self.imported_file.path
with open(filename, 'r') as f:
reader = unicodecsv.reader(
- f, encoding=self.encoding)
+ f, encoding=self.encoding, delimiter=str(self.csv_sep))
nb = sum(1 for row in reader) - self.skip_lines
self.number_of_line = nb
self.save()
@@ -1040,9 +1048,9 @@ class Import(models.Model):
for encoding in encodings:
try:
with open(imported_file) as csv_file:
- vals = [line
- for line in unicodecsv.reader(csv_file,
- encoding=encoding)]
+ vals = [line for line in unicodecsv.reader(
+ csv_file, encoding=encoding,
+ delimiter=str(self.csv_sep))]
if tmpdir:
shutil.rmtree(tmpdir)
return vals