I created a table with three fields but there are no values in these fields. Here is the screenshot of these fields.
I want to add a new row with zeros for all these three fields. Here is my code.
with arcpy.da.UpdateCursor(selectionname,("OBJECTID","FREQUENCY","SUM_Shape_Area")) as cursor:
for row in cursor:
if row[0] == None:
row[0] = 0
cursor.updateRow(row)
else:
print selectionname + " is not empty"
Selectionname is my table, a parameter defined prior to this part of my code. First, I need to check if these three fields are empty, then adding a new row with zeros if they are empty. After several tries, I still could not add a new row to the table.
-
4To add a new row you need to be using an InsertCursor.PolyGeo– PolyGeo ♦2019年04月11日 02:46:21 +00:00Commented Apr 11, 2019 at 2:46
1 Answer 1
Use GetCount to check if table is empty and da.InsertCursor to insert a row. I dont think you should try to set objectid, let it be set automatically. If you want your own ID then add another field for it.
import arcpy
table = 'C:\data.gdb\table123' #Change
fields = ['freq','sum'] #Change
if int(arcpy.GetCount_management(table).getOutput(0)) == 0:
icur = arcpy.da.InsertCursor(table,fields)
icur.insertRow([0,0])
del icur
-
1this is what I want and really appreciate your help! @BERAZhenyu– Zhenyu2019年04月11日 19:43:17 +00:00Commented Apr 11, 2019 at 19:43