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:
- Is there a simpler way how to access values in DBF table?
- Is there a way how to access values in DBF table using SearchCursor and without knowing field (column) names?
Best Answer
You are approching this wrong. By doing
row.field_names[1]
you are asking python to give you thefield_names
method on therow
object which there isn't one.Try this:
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?