I have 1 csv with municipalities and their corresponding zip codes and a GDB of houses which contain address, municipality but no zip codes

Objective: add the zip codes to the GDB table

import csv
import arcpy
import arcgisscripting
arcpy.env.overwriteOutput = True
gp = arcgisscripting.create()
file = "path\\munzip.csv"
houses = "path\\geocoding.gdb\\houses"
csvfile = gp.SearchCursor(file) #personally prefer using the object search cursor than arcpy.da
munzip = {}
for x in csvfile:
    mun = x.MUN.split()
    zip = "0"+str(x.ZIP)
    mun = str(mun[0])
    munzip[mun] = zip
with arcpy.da.UpdateCursor(houses, ("City","ZIP")) as cur:
    for row in cur:
        for k,v in munzip.items():
            if k == str(row[0]):
  • Insert cursor is for inserting new features to a layer. It sounds like you are just updating or adding the zip value? – artwork21 May 11 '16 at 17:10
  • yes that is correct. when I used the update cursor it gave me an error saying: stop iteration. not sure what I am doing wrong – ziggy May 11 '16 at 17:20
  • 1
    You may want to edit this question and change the question/code sample to that focus. – artwork21 May 11 '16 at 17:24
  • you're not stepping through your cursor. with cur.updateRow(v) you're trying to update a row that it's not looking at. – Midavalo May 11 '16 at 18:10
  • 1
    @ziggy your code needs to be reorganised a bit - I'm just heading to the office so if nobody has got to it in the next hour I'll post an answer. Basically you want to load your municipalities and zip into a dictionary, then loop an updatecursor over your gdb table and look up the dictionary for each zip code. – Midavalo May 11 '16 at 18:24

You need to load all your municipalities and zip codes from your CSV into a dictionary, and then in an update cursor loop through your GDB table, look up each municipality in the dictionary and write the corresponding dictionary zip value into the ZIP column of your GDB table.

This assumes columns in your CSV called "City" and "ZIP".

import arcpy

arcpy.env.overwriteOutput = True

file = r"path\\munzip.csv"
houses = r"path\\geocoding.gdb\\houses"

zipDict = {}

with arcpy.da.SearchCursor(file, ['City', 'ZIP']) as cursor:
    for row in cursor:
        zipDict[row[0]] = row[1]

with arcpy.da.UpdateCursor(houses, ['City', 'ZIP']) as cursor:
    for row in cursor:
        if row[0] in zipDict:
            row[1] = zipDict[row[0]]
  • 2
    +1. For fun, you can use dict comprehensions with cursors for a handy 1 liner: zipDict = {row[0] : row[1] for row in arcpy.da.SearchCursor(file, ['City', 'ZIP'])} Granted, you'll probably have better luck with csv files if you use DictReader. – Paul May 11 '16 at 23:33
  • 1
    list comprehension is clean and efficient but i honestly prefer to write it all out, makes it easier to read even if it takes up more lines of code. although as i improve i will probably start to use it more – ziggy May 11 '16 at 23:39
  • I'm with ziggy on that one as well, I also prefer it all written out just due to the fact that I find it easier to read through later – Midavalo May 11 '16 at 23:40
  • Or if you want to get real fancy along the lines of what @Paul did, you can do this zipDict = dict(r for r in arcpy.da.SearchCursor(file, ['City', 'ZIP'])). – crmackey May 12 '16 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.