Skip to content

Instantly share code, notes, and snippets.

@tannerjt
Created April 21, 2017 00:52
Show Gist options
  • Save tannerjt/78901a3ad9632212768f52d6f8457968 to your computer and use it in GitHub Desktop.
Save tannerjt/78901a3ad9632212768f52d6f8457968 to your computer and use it in GitHub Desktop.
# Joshua Tanner
# Loop through a CSV and Feature Class
# to update the fields
import csvkit, os
inCSV = "\\location\\of\\file"
data = {}
indexfield = "id"
reader = csvkit.py3.CSVKitDictReader(open(inCSV))
readerindex = reader.fieldnames.index(indexfield)
mxd = arcpy.mp.ArcGISProject('current')
map = mxd.listMaps()[0]
layer = map.listLayers()[0]
edit = arcpy.da.Editor(os.path.dirname(layer.dataSource))
edit.startEditing(False, True)
def updateRow(myrow):
if(myrow[readerindex] is not None):
reader = csvkit.py3.CSVKitDictReader(open(inCSV))
for record in reader:
if int(record[indexfield]) == int(myrow[readerindex]):
for idx, field in enumerate(reader.fieldnames):
myrow[idx] = record[field]
return myrow
with arcpy.da.UpdateCursor(layer, reader.fieldnames) as cursor:
for row in cursor:
updatedrow = updateRow(row)
cursor.updateRow(updatedrow)
edit.stopEditing(True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment