math.ceil([field]/100)*100
Solved! Go to Solution.
import arcpy import os, math from arcpy import env in_workspace = r"F:/Inventory_export_dev/testRnd" fcList = [] for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"): for filename in filenames: arcpy.AddField_management(os.path.join(dirpath, filename), fieldName, "SHORT", "", "", "", "", "NULLABLE") arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON")
Deleted my last post after re-reading.
However, it looks like you can use one for loop to replace all three that you are running.
might try something like:import arcpy import os, math from arcpy import env in_workspace = r"F:/Inventory_export_dev/testRnd" fcList = [] for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"): for filename in filenames: arcpy.AddField_management(os.path.join(dirpath, filename), fieldName, "SHORT", "", "", "", "", "NULLABLE") arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON")
No need to put the filenames in a list if all you are going to do is iterate through them again. Just as well do the work on the first iteration.
Of course, this will errror out if any of the FC's in your in_workspace do not have the numeric ELEVATION field.
R_
for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"): for filename in filenames: if "ELEVATION" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]: arcpy.AddField_management(os.path.join(dirpath, filename), "SL_ELEV", "SHORT", "", "", "", "", "NULLABLE") arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON") else: print "no Elevation field for dataset ", filename
Replace the work part with this should do it:for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"): for filename in filenames: if "ELEVATION" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]: arcpy.AddField_management(os.path.join(dirpath, filename), "SL_ELEV", "SHORT", "", "", "", "", "NULLABLE") arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON") else: print "no Elevation field for dataset ", filename
Of course, you can skip the else: part, only needed if you want it to do something when ELEVATION field not present.
R_
if "ELEVATION" == arcpy.ListFields(os.path.join(dirpath, filename)):
fieldList = arcpy.ListFields(os.path.join(dirpath, filename)) for f in fieldList: if f.name == "ELEVATION": arcpy.AddField_management(os.path.join(dirpath, filename), "SL_ELEV", "SHORT", "", "", "", "", "NULLABLE") arcpy.CalculateField_management(os.path.join(dirpath, filename), "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON")