import_zips.py 864 B

12345678910111213141516171819202122232425262728
  1. # intended to run in shell plus
  2. import csv
  3. from django.db import connection
  4. from catalog.models import ZipCode
  5. ADMIN_USERNAME = 'jcosta'
  6. PATH_TO_FILE = "/Users/jcosta/projects/pbp_store/pbp_store/catalog/extra/hancock_only_zips"
  7. user = User.objects.get(username=ADMIN_USERNAME)
  8. cr = csv.reader(open(PATH_TO_FILE, "rb"))
  9. for row in cr:
  10. try:
  11. try:
  12. zc = ZipCode.objects.get(zipcode=row[0])
  13. except ZipCode.DoesNotExist:
  14. zc = ZipCode.objects.create(zipcode=row[0], created_by_id=user.id, last_updated_by_id=user.id)
  15. zc.city=row[1]
  16. zc.state=row[2]
  17. zc.county=row[3]
  18. zc.save()
  19. if zc.id % 1000 == 0:
  20. print "Processed zip code: {0}".format(zc.zipcode)
  21. except Exception, e:
  22. connection._rollback()
  23. print "Another exception: {0}".format(e)
  24. print row