Running a for loop over an empty cursor

463
3
Jump to solution
03-11-2013 11:57 AM
StormwaterWater_Resources
New Contributor III
ArcStandard 10.1

I have the following snippet in my script (with an intentionally empty cursor via where-clause):

271    rows = arcpy.da.SearchCursor(fc, Fields, "1=2") 272    arcpy.AddMessage(str(rows)) 273    arcpy.AddMessage("Before") 274    for row in rows: 275        arcpy.AddMessage("In")


When I run it I get this:

Running script UpDownTest... <da.SearchCursor object at 0x1758E700> Before  Traceback (most recent call last):   File "C:\Projects\UpstreamDownstream\UpDownStorm.py", line 274, in <module>     for row in rows: RuntimeError: A column was specified that does not exist.  Failed to execute (UpDownTest).


I'd thought before I started debugging that maybe I had a field name misspelled, but from printing the cursor I can see that an object is returned (though we know it's an empty cursor), and I can see I'm printing text after the cursor is created.  Thus it seems like the error really is in the for loop.  I can't figure out what's wrong here.  Shouldn't the for loop just be passed over?  Any suggestions?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Check your field names held by your Fields list variable again. Print out rows.fields to make sure.

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
Check your field names held by your Fields list variable again. Print out rows.fields to make sure.
0 Kudos
StormwaterWater_Resources
New Contributor III
Check your field names held by your Fields list variable again. Print out rows.fields to make sure.


Sure enough, programmer error, but that was a particularly obscure typo (which has existed undetected in my table for months).  I also would have thought that the updateCursor call would have been where it choked because that's where I first provide the invalid field name.  I guess then the default is to ignore fields it can't find.

On a different topic, do you know how to change the thread icon from a red 'Q' to a green 'A'?  Or better how to delete a thread entirely?
0 Kudos
curtvprice
MVP Esteemed Contributor
On a different topic, do you know how to change the thread icon from a red 'Q' to a green 'A'?


This happens then you check a post as your answer by setting the big checkmark.

Or better how to delete a thread entirely?


Only the uber-moderators can do this. This thread is likely here forever.
0 Kudos