1

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]):
 row[1]=v
 cur.updateRow(row)
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 11, 2016 at 17:02
9
  • Insert cursor is for inserting new features to a layer. It sounds like you are just updating or adding the zip value? Commented May 11, 2016 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 Commented May 11, 2016 at 17:20
  • 1
    You may want to edit this question and change the question/code sample to that focus. Commented May 11, 2016 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. Commented May 11, 2016 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. Commented May 11, 2016 at 18:24

1 Answer 1

4

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]]
 cursor.updateRow(row)
answered May 11, 2016 at 19:23
4
  • 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. Commented May 11, 2016 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 Commented May 11, 2016 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 Commented May 11, 2016 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'])). Commented May 12, 2016 at 0:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.