pythondjangodjango-import-export

Deleting Tablib Dataset Column Headers skipping unexpectedly in Django Import Export


I have a csv file with this sample data:

enter image description here

in which I am trying to modify django-import-export's before_import function and overwrite the dataset, and in order to do so I need to empty the current dataset and replace it with a new one. Here is how I do it:

def before_import(self, dataset, using_transactions, dry_run, **kwargs):
    """
    code to create a new_dataset
    """

    print(dataset.headers)
    # delete data rows in dataset
    del dataset[0:len(dataset)]
    # delete headers
    for header in dataset.headers:
        del dataset[header]

    print(dataset)

    """
    code to replace dataset with the new_dataset
    """

what the shown code does is delete the dataset's data and its headers. But the print shows not all headers are deleted.

enter image description here

It skips headers being deleted, or more accurately, it alternates in deleting them.

Is there something I'm missing in here?


Solution

  • It causes some kind of memory error when you delete the contents of an array inside a FOR LOOP that also uses that array as the reference for the for loop.

    For good measure, copy the array first:

    import copy
    ...
    reference_array = copy.copy(dataset.headers)
    

    Then use this as the reference for your for loop deletion

    for header in reference_array:
         del dataset[header]