Add patchup-helper to assist csv.Sniffer()
This commit is contained in:
parent
c448ce95bd
commit
d75e736be7
@ -13,6 +13,7 @@ from __future__ import with_statement
|
|||||||
|
|
||||||
import csv
|
import csv
|
||||||
import utils.csv_ext
|
import utils.csv_ext
|
||||||
|
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
@ -455,6 +456,7 @@ def application_import(request):
|
|||||||
applications_modified = 0
|
applications_modified = 0
|
||||||
with transaction.commit_on_success():
|
with transaction.commit_on_success():
|
||||||
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
|
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
|
||||||
|
dialect = utils.csv_ext.patchup(dialect)
|
||||||
request.FILES['csvfile'].seek(0)
|
request.FILES['csvfile'].seek(0)
|
||||||
for (lno, line) in enumerate(csv.reader(request.FILES['csvfile'], dialect=dialect)):
|
for (lno, line) in enumerate(csv.reader(request.FILES['csvfile'], dialect=dialect)):
|
||||||
# basic input verification
|
# basic input verification
|
||||||
|
@ -319,6 +319,7 @@ def user_import(request):
|
|||||||
profile.delete()
|
profile.delete()
|
||||||
i = -1
|
i = -1
|
||||||
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
|
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
|
||||||
|
dialect = utils.csv_ext.patchup(dialect)
|
||||||
request.FILES['csvfile'].seek(0)
|
request.FILES['csvfile'].seek(0)
|
||||||
for line in csv.reader(request.FILES['csvfile'], dialect=dialect):
|
for line in csv.reader(request.FILES['csvfile'], dialect=dialect):
|
||||||
i += 1
|
i += 1
|
||||||
|
@ -12,9 +12,21 @@
|
|||||||
|
|
||||||
from csv import Dialect, excel, register_dialect
|
from csv import Dialect, excel, register_dialect
|
||||||
|
|
||||||
class excel_semikolon(excel):
|
class excel_semikolon(Dialect):
|
||||||
delimiter = ';'
|
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)
|
register_dialect("excel_semikolon", excel_semikolon)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user