Tuesday, November 29, 2011

Re: Bulk import of data

On 11/26/2011 11:44 PM, Petr Přikryl wrote:
>>>> import csv
>>>> data = csv.reader(open('/path/to/csv', 'r'), delimiter=';')
>>>> for row in data:
>>>> category = Category.objects.get_or_create(name=row[0])
>>>> sub_category = SubCategory.objects.get_or_create(name=row[1],
>>>> defaults={'parent_category': category})
>>>> product = Product.objects.get_or_create(name=row[2],
>>>> defaults={'sub_category': sub_category})
> There are few potential problems with the cvs as used here.
> Firstly, the file should be opened in binary mode. In Unix-based
> systems, the binary mode is technically similar to text mode.
> However, you may once observe problems when you move
> the code to another environment (Windows).
> Secondly, the opened file should always be closed -- especially
> when building application (web) that may run for a long time.
> You can do it like this:
> ...
> f = open('/path/to/csv', 'rb')
> data = csv.reader(f, delimiter=';')
> for ...
> ...
> f.close()
> Or you can use the new Python construct "with".
> P.

Hey Petr! Thank you so much, I immediately followed your advice. File is
closed at the end of the story.

Cheers, Fabio.

Fabio Natali

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment