for look for ListFields

2005
4
12-19-2013 07:31 AM
Ryan_Galbraith
New Contributor III
I am looking to create a for look to loop through all my ListFields and the Field name is one of the parameters in a Spatial Statistic tool.  Can anyone help please....

I am having trouble understanind what variable to declare.
0 Kudos
4 Replies
by Anonymous User
Not applicable
Original User: hua17

I might be misunderstanding, but if you want to loop through all field names in a feature class, you can use arcpy.ListFields.

Something like this might work:
import arcpy

allFields=arcpy.ListFields("C://Path//To//Data.shp")

for field in allFields:
    print "Field Name: "+ field.name
    #Do stuff here


Let me know if that helps at all.
~Josh
0 Kudos
Ryan_Galbraith
New Contributor III
thank you, thank you, thank you!


import arcpy

arcpy.env.workspace = r"C:\StatsCanada.gdb\Saskatchewan"
featureclass = ("PopulatedCentres")
allFields=arcpy.ListFields(featureclass)

for field in allFields:
    arcpy.SpatialAutocorrelation_stats(featureclass, field.name,"GENERATE_REPORT","INVERSE_DISTANCE","EUCLIDEAN_DISTANCE","NONE","#","#")
   
   
So I got this, but it doesn't like the fields at the beginning of the fields.  Any idea how to just get the fields with numbers in them?
0 Kudos
by Anonymous User
Not applicable
Original User: hua17

Try using "field.type", like this:

import arcpy

arcpy.env.workspace = r"C:\StatsCanada.gdb\Saskatchewan"
featureclass = ("PopulatedCentres")

allFields=arcpy.ListFields(featureclass)

for field in allFields:
    if field.type in ["Double","Integer","SmallInteger"]:
        arcpy.SpatialAutocorrelation_stats(featureclass, field.name,"GENERATE_REPORT","INVERSE_DISTANCE","EUCLIDEAN_DISTANCE","NONE","#","#")

If you want a bit more information on all the field types, you can find it here.
Hope it helps!
0 Kudos
Ryan_Galbraith
New Contributor III
Oh man, that work - perfectly!
0 Kudos