ClassList = ["Forest", "Vegetation"] FieldList = ["Land_Acres","Forest_Acres", "Forest_Percent", "Veg_Acres", "Veg_Percent"] m2ac = 0.0002471054 FcList = arcpy.ListFeatureClasses("*", "") for fc in FcList: areaTab1 = "Metrics_" + os.path.basename(fc) for field in FieldList: arcpy.AddField_management(fc, field, "DOUBLE") TabulateArea(fc, "GID", LC, "Class", areaTab1 , 1) #"UTC_Metrics_" + os.path.basename(fc) arcpy.JoinField_management(fc, "GID", areaTab1, "GID", ClassList)#"trees11_tab_" + os.path.basename(fc) rows = arcpy.UpdateCursor(fc) print str(fc) for row in rows: row.setValue(FieldList[0], (row.Shape_Area * m2ac))#Total Acres rows.updateRow(row) row.setValue(FieldList[1], row.getValue(ClassList[0]) * m2ac) #Forest Acres rows.updateRow(row) row.setValue(FieldList[2], (row.getValue(FieldList[1]) / row.Land_Acres) * 100) #Forest Percent rows.updateRow(row) row.setValue(FieldList[3], row.getValue(ClassList[1]) * m2ac) #Veg Acres rows.updateRow(row) row.setValue(FieldList[4], (row.getValue(FieldList[3]) / row.Land_Acres) * 100) #Veg Percent rows.updateRow(row) del row del rows
some pointers (without having seen the data):
import arcpy
arcpy.env.workspace = r'G:\users\gis\gbacon\Watershed_Testing\valleyave\WatershedDelineation.gdb'
tbl = "TabSummary"
fldlist = [fld.name for fld in arcpy.ListFields(tbl,"","Double")]
with arcpy.da.UpdateCursor(tbl, fldlist) as cursor:
for row in cursor:
i = 0
for fld in fldlist:
row = row / 43560
i += 1
cursor.updateRow(row)
Haven't tested it though. Since you are changing the input data, test it with a copy of the data. Also don't run the code more than once on the same data...
Kind regards, Xander
This worked nicely! Thank you. In this case, all “double” fields needed the update. What would change if I had some “double” fields that needed to be left alone?
I just checked the forum and found that my table is in fact in the zip file attachment. I apologize for not having posted the code correctly. It seems like there’s a better way to do it.
Hi Greg,
The only file in the ZIP file is an XML file called "TabSummary.dbf.xml" (or at least this is what passed my firewall).
In case you know the names of the double fields you want to maintain untouched, you would have to exclude those from the list of fieldnames. This can be done like this (see code below).
import arcpy
arcpy.env.workspace = r'G:\users\gis\gbacon\Watershed_Testing\valleyave\WatershedDelineation.gdb'
tbl = "TabSummary"
fldlist = [fld.name for fld in arcpy.ListFields(tbl,"","Double")]
flds_exclude = ["myFieldname1", "myFieldname2", "myFieldname3"]
flds_touse = list(set(fldlist) - set(flds_exclude))
with arcpy.da.UpdateCursor(tbl, flds_touse) as cursor:
for row in cursor:
i = 0
for fld in flds_touse:
row = row / 43560
i += 1
cursor.updateRow(row)
Kind regards, Xander