I'm trying to understand an arcpy script and I'm stuck with the following lines:

cur = arcpy.SearchCursor(Tabelle) 
row = cur.next()
while row:
    text1 = row.Objekt
    text2 = row.Text

As far as I understand it I load a table into the variable cur. the variable row moves to the next line of the table. But what are text1 and text2? I don't understand the Notation row.Object.

can someone explain me what happens in line 4 and 5?

  • 4
    You are seeing an old-style cursor, I would dissuade you from using these, note you have an infinite loop situation as there is no row = cur.next() in your while loop; also you're not dismissing the cursor when you're finished with it which will leave a lock on your table. row.Objekt is the value of the field Objekt but this only works if that field exists. Consider using with arcpy.da.SearchCursor (Tabelle,['Objekt','Text']) as cur: then for row in cur: instead then access your values with row[0] for Objekt and row[1] for Text. – Michael Stimson Apr 30 '19 at 7:00

With the old style cursor that is used in this study (the new style cursors are the arcpy.da. cursors), you can parse a table row by row (= line by line) and, for each row, get the value of an attribute field (=column) based on its name.

In the case of your script, 'cur', 'row', 'text1' and 'text2' are the names of variables (you could change these names without affecting your script).

cur is an object used to reads table "tabelle" entity by entity (line by line)

row is an object used to access the content of each line

text1 and text2 are variable (probably string) used to store the content of one cell (a specific column of a given row)

"Tabelle" is also a variable, so it must have been defined above this code snippet. However, Objekt and Text must be field names that already exist in the table "tabelle". Writing 'row.objekt' is equivalent to 'row.getValue("objekt")' If you use a table with other field names, those names must be adapted. Note that, if you don't do anything with text1 and text2 inside the "while" loop, then text1 and text2 will only store the values of "text" and "objekt" for the last row of your table, because each assignment to an existing variable overwrites the previous value.

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.