math.ceil([field]/100)*100
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")
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")
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_
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
if "ELEVATION" == arcpy.ListFields(os.path.join(dirpath, filename)):
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_
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: fcList.append(os.path.join(dirpath, filename)) for list in fcList: arcpy.AddField_management(list, fieldName, "SHORT", "", "", "", "", "NULLABLE") arcpy.CalculateField_management(list, "SL_ELEV", "int(round( !ELEVATION!, -2))", "PYTHON")
That method works fine for me. Are you sure your field is the right type (long int)?
import arcpy import os, math from arcpy import env in_workspace = r"F:/Inventory_export_dev/testRnd" rndValue = "int(round(!ELEVATION!, -2))" fcList = [] fieldName = "SL_ELEV" for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"): for filename in filenames: fcList.append(os.path.join(dirpath, filename)) for list in fcList: arcpy.AddField_management(list, fieldName, "SHORT", "", "", "", "", "NULLABLE") print "Rounding ELEVATION field to SL_ELEV" #Rounds values from [ELEVATION] to [SL_ELEV] for elev in fcList: arcpy.CalculateField_management(elev, fieldName, rndValue, "PYTHON")
You would use a negative number for the second round() parameter of course. Use int() to ensure it returns an integer instead of float.int(round([field], -2))
int(round([field], -2))
rndValue = "int(round(!ELEVATION!, -2))" ... arcpy.CalculateField_management(elev, fieldName, rndValue, "PYTHON")
How I mean to phrase this question is, how to round a number (say 11) to 0 instead of 100?
I am a little confused about how to round a field to the nearest 100th. Would I use math.ceil() or the round()? math.ceil([field]/100)*100 Does this only round down though?Thanks
round([field], 2)
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.