Add patchup-helper to assist csv.Sniffer()

This commit is contained in:
rene 2011-11-24 19:38:25 +01:00
parent c448ce95bd
commit d75e736be7
3 changed files with 17 additions and 2 deletions

View File

@ -13,6 +13,7 @@ from __future__ import with_statement
import csv
import utils.csv_ext
from django.shortcuts import redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
@ -455,6 +456,7 @@ def application_import(request):
applications_modified = 0
with transaction.commit_on_success():
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
dialect = utils.csv_ext.patchup(dialect)
request.FILES['csvfile'].seek(0)
for (lno, line) in enumerate(csv.reader(request.FILES['csvfile'], dialect=dialect)):
# basic input verification

View File

@ -319,6 +319,7 @@ def user_import(request):
profile.delete()
i = -1
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
dialect = utils.csv_ext.patchup(dialect)
request.FILES['csvfile'].seek(0)
for line in csv.reader(request.FILES['csvfile'], dialect=dialect):
i += 1

View File

@ -12,9 +12,21 @@
from csv import Dialect, excel, register_dialect
class excel_semikolon(excel):
delimiter = ';'
class excel_semikolon(Dialect):
delimiter = ';'
doublequote = True
lineterminator = '\r\n'
quotechar = '"'
quoting = 0
skipinitialspace = False
def patchup(dialect):
if dialect:
if dialect.delimiter == excel_semikolon.delimiter and \
dialect.quotechar == excel_semikolon.quotechar:
# walks like a duck and talks like a duck.. must be one
dialect.doublequote = True
return dialect
register_dialect("excel_semikolon", excel_semikolon)