leftieant76

Using variables inside arcpy functions

Discussion created by leftieant76 on Aug 16, 2012
Latest reply on Aug 16, 2012 by leftieant76
Hello

Caution... Python rookie here.

I am sure this will be a very easy answer, but for the life of me I can't figure it out.

I am trying to write an iterator that will cycle through the features within a feature class, checking each of them against a values layer.

I have almost worked out how to run the iteration by identifying the OID field and the number of features, and then run a while: loop, but I have struck a problem in creating the temporary layer that will house the single feature for checking.

Code below:

# new code using OID and while loop:

num_feats = int(str((arcpy.GetCount_management(dataset_tocheck)))) #returns number of features in dataset to check. Initial result is type RETURN, covert to STRING and then to INTEGER.
descFC = arcpy.Describe(dataset_tocheck) #set descFC variable for dataset to check
OIDFieldName = str(descFC.OIDFieldName) #find OID fieldname of dataset to check, convert to STRING

oid = 0 #create OID variable for WHILE loop

arcpy.MakeFeatureLayer_management(dataset_tocheck,"dataset_tocheck_lyr") #make feature layer from feature class

while oid < num_feats: #create WHILE loop for OID in [0, num_feats]
    dataset_tocheck_subset = arcpy.SelectLayerByAttribute_management("dataset_tocheck_lyr", "NEW_SELECTION", OIDFieldName = oid)
    oid = oid + 1


The issue I am striking is using the variables OIDFieldName and oid in the SelectLayerByAttribute function. I am trying to use variables as I want to make this code as portable as possible and once it is running, assemble into a tool for corporate deployment.

Error being returned:

Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "U:\PythonValuesChecking\Python Scripts\ValuesChecker.py", line 70, in <module>
    dataset_tocheck_subset = arcpy.SelectLayerByAttribute_management("dataset_tocheck_lyr", "NEW_SELECTION", OIDFieldName = oid)
TypeError: SelectLayerByAttribute() got an unexpected keyword argument 'OIDFieldName'


Am I able to use variables in this manner? What am I doing wrong?

Outcomes