local variable 'row' referenced before assignment

4507
3
Jump to solution
01-09-2016 12:13 PM
ben_abdallahmhd
New Contributor III

Hi all,

when i run this code the message     "local variable 'row' referenced before assignment"         appear but i think the name o the

variable and the indentation are correct.

tb = table

            if tb is "RRAXERCL":

                fieldsToConcat_RRAXERCL = ["CODECAVA","CODCLASS","TYPERTCR","PROFILRC","ETATRTCR"]

                cursorup = arcpy.da.UpdateCursor(table,fieldsToConcat_RRAXERCL)

                for row in cursorup :

                   row[0] = str(row[1]) + str(row[2]).rjust(2,'0') + str(row[3]).rjust(2,'0') + str(row[4]).rjust(2,'0') + str('00')

                    cursorup.updateRow(row)

                del row

                del cursorup

thank you

0 Kudos
1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Emeritus

Just a guess, since it is not formatted     (see Posting Code blocks in the new GeoNet​)  But my guess is that the

del row

is what is causing the issue. ....

edit...mayeb becaue it is not returning anything for cursorup....or more likely, I don't think you even need to delete the row   with .da

And depending on what ArcGIS version you are using, you may want to review Accessing data using cursors—Help | ArcGIS for Desktop  for your cursor.

View solution in original post

0 Kudos
3 Replies
RebeccaStrauch__GISP
MVP Emeritus

Just a guess, since it is not formatted     (see Posting Code blocks in the new GeoNet​)  But my guess is that the

del row

is what is causing the issue. ....

edit...mayeb becaue it is not returning anything for cursorup....or more likely, I don't think you even need to delete the row   with .da

And depending on what ArcGIS version you are using, you may want to review Accessing data using cursors—Help | ArcGIS for Desktop  for your cursor.

0 Kudos
ben_abdallahmhd
New Contributor III

hi rebecca thank you very much,

but why del don't work here. Iuse it to clean the processus of running code

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

There is some info regarding locks in the help doc I included above....a snippet from it is

In Python, the lock persists until the cursor is released. Otherwise, all other applications or scripts could be unnecessarily prevented from accessing a dataset. A cursor can released by one of the following:

  • Including the cursor inside a with statement, which will guarantee the release of locks regardless of whether or not the cursor is successfully completed
  • Calling reset() on the cursor
  • The completion of the cursor
  • Explicitly deleting the cursor using Python's del statement

An edit session in ArcMap applies a shared lock to data during the edit session. An exclusive lock is applied when edits are saved. A dataset is not editable if an exclusive lock already exists.

Using the first type, the "with" statement, tends to be the preferred method I've seen in the forums.  There are some good examples showing this on that same help document.  I hope that helps explain "why". 

0 Kudos