add possibility to create a dialect for the csv file

This commit is contained in:
florian-dacosta
2014-12-10 18:44:29 +01:00
parent 0a20feecaa
commit 8bb62071f3
2 changed files with 11 additions and 5 deletions

View File

@@ -41,7 +41,7 @@ class FileParser(BankStatementImportParser):
""" """
def __init__(self, parse_name, ftype='csv', extra_fields=None, header=None, def __init__(self, parse_name, ftype='csv', extra_fields=None, header=None,
**kwargs): dialect=None, **kwargs):
""" """
:param char: parse_name: The name of the parser :param char: parse_name: The name of the parser
:param char: ftype: extension of the file (could be csv, xls or :param char: ftype: extension of the file (could be csv, xls or
@@ -64,6 +64,7 @@ class FileParser(BankStatementImportParser):
self._datemode = 0 # used only for xls documents, self._datemode = 0 # used only for xls documents,
# 0 means Windows mode (1900 based dates). # 0 means Windows mode (1900 based dates).
# Set in _parse_xls, from the contents of the file # Set in _parse_xls, from the contents of the file
self.dialect = dialect
def _custom_format(self, *args, **kwargs): def _custom_format(self, *args, **kwargs):
"""No other work on data are needed in this parser.""" """No other work on data are needed in this parser."""
@@ -111,7 +112,8 @@ class FileParser(BankStatementImportParser):
csv_file.write(self.filebuffer) csv_file.write(self.filebuffer)
csv_file.flush() csv_file.flush()
with open(csv_file.name, 'rU') as fobj: with open(csv_file.name, 'rU') as fobj:
reader = UnicodeDictReader(fobj, fieldnames=self.fieldnames) reader = UnicodeDictReader(fobj, fieldnames=self.fieldnames,
dialect=self.dialect)
return list(reader) return list(reader)
def _parse_xls(self): def _parse_xls(self):

View File

@@ -29,11 +29,15 @@ def UnicodeDictReader(utf8_data, **kwargs):
pos = utf8_data.tell() pos = utf8_data.tell()
sample_data = utf8_data.read(2048) sample_data = utf8_data.read(2048)
utf8_data.seek(pos) utf8_data.seek(pos)
if not kwargs.get('dialect'):
dialect = sniffer.sniff(sample_data, delimiters=',;\t') dialect = sniffer.sniff(sample_data, delimiters=',;\t')
del kwargs['dialect']
else:
dialect = kwargs.pop('dialect')
csv_reader = csv.DictReader(utf8_data, dialect=dialect, **kwargs) csv_reader = csv.DictReader(utf8_data, dialect=dialect, **kwargs)
for row in csv_reader: for row in csv_reader:
yield dict([(key, unicode(value, 'utf-8')) for key, value in yield dict([(unicode(key, 'utf-8'), unicode(value, 'utf-8'))
row.iteritems()]) for key, value in row.iteritems() if key])
class BankStatementImportParser(object): class BankStatementImportParser(object):