beancount / beangulp

Importers framework for Beancount
GNU General Public License v2.0
57 stars 23 forks source link

Let CSV importer support tab-separated files #85

Open l9i opened 4 years ago

l9i commented 4 years ago

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.

jarofromel commented 4 years ago

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')]
dnicolodi commented 3 years ago

This issue should be moved to beangulp.

l9i commented 3 years ago

@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

dnicolodi commented 3 years ago

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.