Make SearchCursor from Excel table?

360
3
Jump to solution
10-01-2012 03:08 PM
RobertMartin2
Occasional Contributor II
Hi all,

I am having a heck of a time making a SearchCursor from an Excel table with a WHERE query. I made a test table with just one column called NAME and a cursor as follows:

where = '\"NAME\" = \'Tom\'' rows = arcpy.SearchCursor("Sheet1$", where)


This works fine. But when I loop over the rows and print NAME nothing happens (I'm guessing no rows satisfy the query, though the data is there). Conversely if I change the statement to:

where = '\"NAME\" <> \'Tom\''


it returns ALL rows. Obviously the WHERE clause is not working, but it looks fine to me and I don't get any syntax errors. Can anyone think of what might be happening here? Maybe an Excel peculiarity?


Thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Try placing the field name in brackets.  Ex:

where = "[First Name] = 'Tom'"

View solution in original post

3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Robert,

Try changing your where clause to:

where = "NAME = 'Tom'"
0 Kudos
RobertMartin2
Occasional Contributor II
Thanks Jake, that did the trick for that table. The only issue is that my actual data has spaces in the column names, so I think I need the quotes. Any thoughts on how to incorporate those?

I tried something like
where = "\"FIRST NAME\" = 'Tom'"

and the cursor generated fine, but with no rows.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try placing the field name in brackets.  Ex:

where = "[First Name] = 'Tom'"