import_zips.py 908 B

1234567891011121314151617181920212223242526272829303132
  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 = (
  7. "/Users/jcosta/projects/pbp_store/pbp_store/catalog/extra/hancock_only_zips"
  8. )
  9. user = User.objects.get(username=ADMIN_USERNAME)
  10. cr = csv.reader(open(PATH_TO_FILE, "rb"))
  11. for row in cr:
  12. try:
  13. try:
  14. zc = ZipCode.objects.get(zipcode=row[0])
  15. except ZipCode.DoesNotExist:
  16. zc = ZipCode.objects.create(
  17. zipcode=row[0], created_by_id=user.id, last_updated_by_id=user.id
  18. )
  19. zc.city = row[1]
  20. zc.state = row[2]
  21. zc.county = row[3]
  22. zc.save()
  23. if zc.id % 1000 == 0:
  24. print "Processed zip code: {0}".format(zc.zipcode)
  25. except Exception, e:
  26. connection._rollback()
  27. print "Another exception: {0}".format(e)
  28. print row