3

I am very new to ArcPy and Python in general. I want write a script to read attribute table from a feature class and merge some data from a csv file into the table. Right now, I am just playing around with the table just to make sure everything is fine, so here is the code:

fc = 'ShuttleStop_nextbus'
cursor = arcpy.SearchCursor(fc, '*', """ 'NextBus_nu' = '27837'  """)
for row in cursor:
     print row

(I have set arcpy.env.workspace to current workspace)

So I inspected the content of the feature class with

len(arcpy.ListFields("ShuttleStop_nextbus"))

It return 13, which is exactly the number of fields. Then I read all the fields with

fields = arcpy.ListFields("ShuttleStop_nextbus")
for f in fields: print f 

Then, it outputs something like this:

<geoprocessing describe field object object at 0x24546980>

<geoprocessing describe field object object at 0x24546DE8>

<geoprocessing describe field object object at 0x24546E78>

 .........
 .........

Turns out the fields are actually of type Describe.

Is there anyway that I can query the table if all the above occurs? I still want to use the "where" clause of SearchCursor(). Let me know if I need to clarify something.

3
  • i don't what the question is. you want to print the values in the fields?
    – gotchula
    Commented Apr 3, 2013 at 22:01
  • Yup, pretty much. So basically i can do something like this: 'cursor = arcpy.SearchCursor(fc) for row in cursor: print row.getValue('Name') ' But I still wanna use the "where" clause for querying. Commented Apr 3, 2013 at 22:03
  • 1
    go to the online doc, there's python code examples with most topics. resources.arcgis.com/en/help/main/10.1/#/SearchCursor/… don't use arcpy.SearchCursor, use arcpy.da.SearchCursor.
    – gotchula
    Commented Apr 3, 2013 at 22:19

3 Answers 3

11

Confusingly, there are two different types of cursors: the "classic" cursors of Arc 10.0 and the arcpy.da cursors of Arc 10.1.


Arc 10.0 - "Classic" cursors

  • When using classic cursors (i.e. arcpy.SearchCursor) the cursor object is an iterable containing row objects. You need to use the getValue method to retrieve the data from the row.
  • The second parameter for arcpy.SearchCursor is your where clause, so you don't need the "*" parameter in there.
  • Your where clause is not set up properly, since Arc needs to have field names delimited by double quotes. To handle this with Python's use of quotes as string delimiters, you need to use the backslash escape character.

Here's a code sample with fixes applied:

fc = 'ShuttleStop_nextbus'
#List comprehension creates list of field names to read
fieldsList = [field.name for field in arcpy.ListFields("ShuttleStop_nextbus")]
cursor = arcpy.SearchCursor(fc, "\"NextBus_nu\" = '27837'")
for row in cursor:
    for field in fieldsList: #Iterate through field names
        print row.getValue(field) #Print value of this field for this row

Arc 10.1 - arcpy.da cursors

  • When using the new cursors in the data access module (i.e. arcpy.da.SearchCursor) you need to pass in a list of field names as the second parameter in the cursor constructor. This requires some more work up-front, but the resulting row objects are Python lists, which makes it easier to read data when iterating through cursor rows. arcpy.da.SearchCursor also has better performance than arcpy.SearchCursor, partially because it skips unimportant fields, especially geometry.
  • As before, your where clause is not set up properly, since Arc needs to have field names delimited by double quotes. To handle this with Python's use of quotes as string delimiters, you need to use the backslash escape character.

Here's a code sample with fixes applied:

fc = 'ShuttleStop_nextbus'
#List comprehension creates list of field names to read
fieldsList = [field.name for field in arcpy.ListFields("ShuttleStop_nextbus")]
cursor = arcpy.da.SearchCursor(fc, fieldsList , "\"NextBus_nu\" = '27837'")
for row in cursor:
    for cell in row: #Iterate through row, which is a Python list
        print cell #Print value of item in row list
5
  • 1
    +1, But perhaps the confusion with the asterisk was that with arcpy.da.SearchCursor, the second parameter is the list of fields you want to access, and specifying an asterisk means to list all fields.
    – blah238
    Commented Apr 3, 2013 at 22:49
  • 1
    Also, not all workspace types will accept double-quotes for the field delimiter -- use AddFieldDelimiters to take out the guesswork (see my answer).
    – blah238
    Commented Apr 3, 2013 at 22:51
  • Sorry that I come back to this question a bit late. So 1) Your answer actually works, but I just have to omit the single quote for the value (so it is \"NextBus_nu\" = 27837, instead of \"NextBus_nu\" = '27837'). Thanks! Commented Apr 5, 2013 at 18:29
  • 2) I'm not sure why, but arcpy.da is not available in my arcmap. I'm currently using AcrMap version 10.0.0.2414 Commented Apr 5, 2013 at 18:36
  • @vandershraaf As I explained, arcpy.da cursors are only available in ArcGIS 10.1 and above. You do not have this. So you have to use the "classic" arcpy cursors.
    – dmahr
    Commented Apr 8, 2013 at 11:44
4

In addition to dmahr's great suggestions, I will just refer you to one of my prior answers which contains a function to help construct your where clause with the appropriate delimiters:

This makes use of the AddFieldDelimiters function to add the appropriate (DBMS-specific) field delimiters, and also wraps text field values in single quotes.

1

If you want the names (or other things you can get from field objects):

for f in fields:
    print f.name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.