Search code examples
django-import-exporttablib

Clean headers and remove bold text


I have a CSV that has bold text on all of the first columns. I wanted to sanitize it first since now it failed to get the row I wanted to get.

I tried printing the row in before_import_row and this is how it looks like.

('\ufeffaccount_number', '000-152-1808')


Solution

  • It is possible using dynamic columns in tablib. Add a callable which returns the unsanitized column value, and then add it to a new column.

    def accno_cleaned(row):
        return '\ufeffaccount_number'
    
    def before_import(self, dataset, using_transactions, dry_run, **kwargs):
        dataset.append_col(accno_cleaned, header='account_number')
    

    However I think it might be better to sanitize the data before it gets imported into django-import-export if you can because this will be easier to maintain in the long run.