方法
ArcGIS Pro では、ArcGIS API for Python を使用して、フィーチャクラスの属性テーブル内のフィールド データ レコードを CSV ファイルから更新するプロセスを自動化できます。 この記事で説明するワークフローに従い、CSV ファイルから属性テーブルを更新して、スタンドアロン Python ファイル (.py) を使用して新しい行を追加します。 Python ファイルは、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>")
以下のコード ブロックに、完全に機能するスクリプトを示します。
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: 000029321
ArcGIS エキスパートのサポートを受ける
今すぐチャットを開始