PROCÉDURE
Dans ArcGIS Pro, ArcGIS API for Python peut être utilisé pour automatiser le processus de mise à jour des enregistrements de données de champs dans la table attributaire d’une classe d’entités à partir d’un fichier CSV. Suivez le processus décrit dans cet article pour mettre à jour la table attributaire à partir d’un fichier CSV et ajouter de nouvelles lignes à l’aide d’un fichier Python autonome (.py). Le fichier Python s’exécute également dans Jupyter Notebook.
import arcpy
fc = r"<featureClassPath>" csv = r"<csvPath>" copy_fields = ["<fieldName1>", "<fieldName2>", "<fieldName3>"]
csv_dict = {row[0]: row for row in arcpy.da.SearchCursor(csv, copy_fields)}
with arcpy.da.UpdateCursor(fc, copy_fields) as cursor: for row in cursor: key = row[0] try: new_row = csv_dict[key] except KeyError: print(f"Entry not found in csv: {copy_fields[0]} = {key}") continue cursor.updateRow(new_row) del csv_dict[key]
with arcpy.da.InsertCursor(fc, copy_fields) as cursor: for new_row in csv_dict.values(): cursor.insertRow(new_row)
print("<message>")
Le bloc de code ci-dessous illustre le script opérationnel complet.
fc = r"C:\Users\testUser\Documents\Article work\Article 29321\MyProject 29321\MyProject 29321.gdb\Schools_all" csv = r"C:\Users\testUser\Documents\Article work\Article 29321\schoolLvl.csv" copy_fields = ["NAME", "LEVEL_NO", "LEVEL"] import arcpy csv_dict = {row[0]: row for row in arcpy.da.SearchCursor(csv, copy_fields)} with arcpy.da.UpdateCursor(fc, copy_fields) as cursor: for row in cursor: key = row[0] try: new_row = csv_dict[key] except KeyError: print(f"Entry not found in csv: {copy_fields[0]} = {key}") continue cursor.updateRow(new_row) del csv_dict[key] with arcpy.da.InsertCursor(fc, copy_fields) as cursor: for new_row in csv_dict.values(): cursor.insertRow(new_row) print("Complete")
ID d’article: 000029321
Obtenir de l’aide auprès des experts ArcGIS
Télécharger l’application Esri Support