Select to view content in your preferred language

How to update attribute table from a .csv and add new rows to apply geometry

1691
2
Jump to solution
12-14-2022 05:46 PM
jgstaley
New Contributor II

Hi, 

I have a feature class that has numerous polygons with a bunch of attribute information. I also have a .csv file that contains the same info as the attribute table but is updated monthly. 

I want to do two things: I want to update the values for the attribute table to reflect any changes that happened in the .csv (IE: change the "status" field from "active" to "expired).  I figure I could use arcpy's update cursor for this (I only have as much experience as the "Creating Python Scripts" course offered by esri).

Second, I want to add new any new rows that are in the .csv file but are not in the attribute table so that I can draw geometry for them. I guess I would need an insert cursor for this?

Thanks!

Thanks!

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

 

# input parameters
fc = r"N:\...\db.gdb\TestPoints"  # path to the fc
csv = r"N:\...\testpoints.csv"  # path to csv
copy_fields = ["TextField", "IntegerField", "DoubleField"]  # list of the fields you want to copy. First field has to be a unique identifer


import arcpy
# read the csv, store its data in a dictionary
csv_dict = {row[0]: row for row in arcpy.da.SearchCursor(csv, copy_fields)}

# loop over the features with an UpdateCursor
with arcpy.da.UpdateCursor(fc, copy_fields) as cursor:
    for row in cursor:
        # get the csv data for that row
        key = row[0]
        try:
            new_row = csv_dict[key]
        except KeyError:  # row not found in csv
            print(f"Entry not found in csv: {copy_fields[0]} = {key}")
#            cursor.deleteRow()  # uncomment this to delete the feature
            continue
        # update the feature
        cursor.updateRow(new_row)
        del csv_dict[key]

# every feature that is left in the csv_dict is new
with arcpy.da.InsertCursor(fc, copy_fields) as cursor:
    for new_row in csv_dict.values():
        cursor.insertRow(new_row)

 


Have a great day!
Johannes

View solution in original post

0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor

 

# input parameters
fc = r"N:\...\db.gdb\TestPoints"  # path to the fc
csv = r"N:\...\testpoints.csv"  # path to csv
copy_fields = ["TextField", "IntegerField", "DoubleField"]  # list of the fields you want to copy. First field has to be a unique identifer


import arcpy
# read the csv, store its data in a dictionary
csv_dict = {row[0]: row for row in arcpy.da.SearchCursor(csv, copy_fields)}

# loop over the features with an UpdateCursor
with arcpy.da.UpdateCursor(fc, copy_fields) as cursor:
    for row in cursor:
        # get the csv data for that row
        key = row[0]
        try:
            new_row = csv_dict[key]
        except KeyError:  # row not found in csv
            print(f"Entry not found in csv: {copy_fields[0]} = {key}")
#            cursor.deleteRow()  # uncomment this to delete the feature
            continue
        # update the feature
        cursor.updateRow(new_row)
        del csv_dict[key]

# every feature that is left in the csv_dict is new
with arcpy.da.InsertCursor(fc, copy_fields) as cursor:
    for new_row in csv_dict.values():
        cursor.insertRow(new_row)

 


Have a great day!
Johannes
0 Kudos
jgstaley
New Contributor II

Thanks so much! This helps a lot

0 Kudos