Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading