I have a CSV file listing items that I need to store within the database.
I need to check which items are not already stored, and if not stored I need to save them within the database.
There are 2-5 million rows.
The model is Django's User model.
I have a CSV file of this form:
Item_ID, Surname, Policy_number, Sex, Title, Start_date
This is the code:
import csv
reader = csv.reader(open('items.csv', 'rb'))
for index, row in enumerate(reader):
if User.objects.filter(username=row[2]).count():
continue
try:
user = User(username=row[2],last_name=row[1],password='*')
user.save()
except Exception, e:
print e
del user
del row
del index
Any method you would recommend?