I am following the example within Editing Features: to select a feature within the feature layer.
sfo_feature = [f for f in ports_features if f.attributes['port_name']=='SAN FRANCISCO'][0]
The code is successful when hard-coding the value, as with 'SAN FRANCISCO' above, but fails with IndexError: list index out of range when using a variable. Following is my code:
The following is successful:
obs_feature = [f for f in obs_features if f.attributes['globalid']=='18d8c3ac-f828-4d93-b4e8-8a3b281f1790'][0]
And, the following is successful:
globalid = '18d8c3ac-f828-4d93-b4e8-8a3b281f1790'
obs_feature = [f for f in obs_features if f.attributes['globalid']==globalid][0]
The following fails, with IndexError: list index out of range when using a variable. I know the value is correct via the use of AddMessage to know the value being queried:
globalid = "'"+original_globalid+"'"
arcpy.AddMessage("globalid: {}".format(globalid)+"; fld: {}".format(fld)+"; fld_value: {}".format(fld_value))
obs_feature = [f for f in obs_features if f.attributes['globalid']==globalid][0]
I assume this is something simple, but can't figure out the problem. I've tried format(globalid), although that shouldn't be necessary, but that fails as well.
Someone, please save me from myself!
Solved! Go to Solution.
Isn't original_globalid already a string with quotes? Are you putting an extra set of quotes around the string?
Isn't original_globalid already a string with quotes? Are you putting an extra set of quotes around the string?
Sorry, no, it's not already a string with quotes. . I should have put the result of the AddMessage:
globalid: '18d8c3ac-f828-4d93-b4e8-8a3b281f1790'; fld: EO_ID; fld_value: 300997.0
So, you can see the globalid from the AddMessage is the same as what was hardcoded in the code that works. It just doesn't work when using the variable.
However, note that the fld value is also a string and doesn't have quotes.
You're a genius! I was obviously over-thinking this. Thank you so much! It always helps to have someone else's view.