beangulp icon indicating copy to clipboard operation
beangulp copied to clipboard

Let CSV importer support tab-separated files

Open l9i opened this issue 4 years ago • 4 comments

Would it be possible for the CSV importer to support tab-separated files as well? Currently the 'text/csv' MIME type seems hardcoded.

While I could monkey patch it with self.remap['mime'] = [re.compile('text/tab-separated-values')] in my subclass' __init__(), it'd be rather ugly.

l9i avatar Sep 12 '20 18:09 l9i

There is an argument to CSV importer - csv_dialect. It allows you to define your own way to parse your csv file (on the positive side, it is the part of the Python standard library), see https://docs.python.org/3/library/csv.html#csv-fmt-params .

Simple example of the configuration file

from beancount.ingest.importers import csv
from csv import register_dialect

register_dialect('own', 'excel', delimiter='\t')
CONFIG = [csv.Importer({csv.Col.DATE: 'Booking Date',
                        csv.Col.PAYEE: 'Beneficiary',
                        csv.Col.AMOUNT:'Amount'},
                       'Assets:CZ:Unicredit:Current', 'CZK',
                       csv_dialect='own')]

jarofromel avatar Sep 25 '20 13:09 jarofromel

This issue should be moved to beangulp.

dnicolodi avatar May 23 '21 16:05 dnicolodi

@jarifuri, I don't think this problem can be solved by passing csv_dialect since the MIME type matcher always expects text/csv: https://github.com/beancount/beangulp/blob/44d235b80cec8d98ed2ba4dfc368b741f3c77abd/beangulp/importers/csv.py#L181

l9i avatar May 28 '21 21:05 l9i

I don't see anything wrong with overriding the mime type matching in a subclass. Anyhow, the this CSV importer is deprecated and replaced by beangulp.importers.csvbase.Importer.

dnicolodi avatar May 29 '21 15:05 dnicolodi