1

I have got the following problem: I would like to access a row from a DBF table. I have tried to access it through SearchCursor, but I am not able to do it without knowing the field name. This is my code:

# Create SearchCursor
rows = arcpy.SearchCursor("tabulka.gdb\zrazkove_uhrny")

# Get names of all fields
field_names = []
sFields=arcpy.ListFields("tabulka.gdb\zrazkove_uhrny")
for field in sFields:
    field_names.append(field.name)

# Print all values of second row
for row in rows:
    print row.field_names[1]

This code works only if I know the name of a row which I don't. I don't even know the number of columns in the table. This code work only if I substitute row.field_names[1] with row.F2 (F2 is the name of the column).

My questions are:

  1. Is there a simpler way how to access values in DBF table?
  2. Is there a way how to access values in DBF table using SearchCursor and without knowing field (column) names?
5

You are approching this wrong. By doing row.field_names[1] you are asking python to give you the field_names method on the row object which there isn't one.

Try this:

field = arcpy.ListFields("tabulka.gdb\zrazkove_uhrny")[1]
name = field.name
for row in rows:
    print row.getValue(name)

I think you might even be able to do row.getValue(1) but I'm not sure as I don't have arcpy on hand. Any Arcpy experts want to confirm or deny that?

  • Ok thank you. It helped. row.getValue(1) doesnt work, but it can be substituted with row.getValue(field_names[1]). Thanks – user21816 Sep 17 '13 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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