Created
April 21, 2017 00:52
-
-
Save tannerjt/78901a3ad9632212768f52d6f8457968 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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