I am trying to run a Python script that loops through all fields in my input feature attribute table. I have searched extensively and tried to use arcpy.ListField to work but it seems all people got stuck on the same issue: we cannot get the columns we are trying to loop to work as input field.
Here is my script:
import arcpy
arcpy.env.workspace = "C:\Users\jacobyan\Desktop\JACOB"
fieldList = arcpy.ListFields("BG_SAR1.shp")
try:
#Call all fields in the list
for field in fieldList:
outputname = str(field.name) + ".shp"
print outputname
arcpy.ClustersOutliers_stats("BG_SAR1.shp", "field", outputname, "GET_SPATIAL_WEIGHTS_FROM_FILE","EUCLIDEAN_DISTANCE", "ROW", "#", "SpatialWeights.swm","NO_FDR")
except:
print arcpy.GetMessages()
The result I got is:
FID.shp Executing: ClustersOutliers C:\Users\jacobyan\Desktop\JACOB\BG_SAR1.shp field C:\Users\jacobyan\Desktop\JACOB\FID.shp GET_SPATIAL_WEIGHTS_FROM_FILE EUCLIDEAN_DISTANCE ROW # C:\Users\jacobyan\Desktop\JACOB\SpatialWeights.swm NO_FDR Start Time: Wed Jul 29 14:45:56 2015 Failed to execute. Parameters are not valid. ERROR 000728: Field field does not exist within table Failed to execute (ClustersOutliers). Failed at Wed Jul 29 14:45:56 2015 (Elapsed Time: 0.03 seconds)
I adjust "field" to "field.name" and try to replace it with FieldList[count], but none of those worked.
Here are two related posts I have looked at: Iterating through columns in table using ModelBuilder?
"field"
withfield.name
. Also, in your question, it would be better if you could format the code by adding four spaces to the beginning of each line with code in it.