Django-import-export: semicolon/comma

Created on 21 Nov 2014  路  6Comments  路  Source: django-import-export/django-import-export

Hi!

I am working on import of csv file - in csv file, i've got field delimeter = semicolon, and when I am importing file i am getting an error:
KeyError: u'Column \'NumeroCommande\' not found in dataset. Available columns are: [u\'NumeroColis;"NumeroCommande";\']'

So it seems, that it takes all the columns and melting it in one.

If there is a comma set as a field_delimeter there is no problem with importing file.

Is there a way to replace semicolon with comma in your code while importing a file?

question solution included stale

Most helpful comment

As an example for @klabedz and anyone else, I just did this to add semicolon CSV support to a project I'm working on based on @chhantyal's and @bmihelac's comments above:

from import_export.formats import base_formats

class SCSV(base_formats.CSV):

    def get_title(self):
        return "scsv"

    def create_dataset(self, in_stream, **kwargs):
        kwargs['delimiter'] = ';'
        return super().create_dataset(in_stream, **kwargs)

# ...

# within the Admin subclass that uses ImportMixin or ImportExportMixin:
    def get_import_formats(self):
        return self.formats + (SCSV,)

So this is simple enough I'm not sure it warrants making an actual code change to django-import-export.

All 6 comments

You can add new formats, but note that csv with delimeter ";" does not exists in tablib

see:

https://github.com/bmihelac/django-import-export/blob/master/import_export/formats/base_formats.py

Looks like tablib now supports custom delimiter, see here https://github.com/kennethreitz/tablib/commit/70716fdd216755dc4b542df74e95b0e5ac74f0ee

As an example for @klabedz and anyone else, I just did this to add semicolon CSV support to a project I'm working on based on @chhantyal's and @bmihelac's comments above:

from import_export.formats import base_formats

class SCSV(base_formats.CSV):

    def get_title(self):
        return "scsv"

    def create_dataset(self, in_stream, **kwargs):
        kwargs['delimiter'] = ';'
        return super().create_dataset(in_stream, **kwargs)

# ...

# within the Admin subclass that uses ImportMixin or ImportExportMixin:
    def get_import_formats(self):
        return self.formats + (SCSV,)

So this is simple enough I'm not sure it warrants making an actual code change to django-import-export.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

The answer of @mgrdcm it's almost good but it doesn't work for me, I've solved it with (based on @mgrdcm answer):
from import_export.formats import base_formats


class SCSV(base_formats.CSV):

    def get_title(self):
        return "scsv"

            def create_dataset(self, in_stream, **kwargs):
                    kwargs['delimiter'] = ';'
                    kwargs['format'] = 'csv'
                    return tablib.import_set(in_stream, **kwargs)

# ...

# within the Admin subclass that uses ImportMixin or ImportExportMixin:
    def get_import_formats(self):
        self.formats += [SCSV]
                return [f for f in self.formats if f().can_import()]

I'm having the following error when I try to import semicolom csv...
"NameError encountered while trying to read file:"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lmeroc picture lmeroc  路  3Comments

lx1054331851 picture lx1054331851  路  5Comments

bestofothers picture bestofothers  路  3Comments

gone picture gone  路  5Comments

PetrDlouhy picture PetrDlouhy  路  3Comments