Skip to main content
added 670 characters in body
Source Link
Michael Stimson
  • 25.7k
  • 2
  • 35
  • 75

Expanding on the comment.

There's two ways set your rows that contain 'reserve' location attribute to 1, but first I must point out your 2nd update cursor does not contain UCur1.updateRow(URow1) so no matter what no rows are updated.

Firstly by where clause:

with arcpy.da.UpdateCursor("in_memory/headphotos",[field2,'Located'],'{} like \'%reserve%\''.format(field2)) as UCur1:
    for URow1 in UCur1:
      URow1[1] = 1           # all rows contain the word 'reserve' in their field2.
      UCur1.updateRow(URow1) # store the updated row

Where can this be a problem? The wildcard character is different for different feature class types.. % is fine most of the time but personal geodatabases use * as a wildcard which can cause horrible problems when reusing code.

Secondly by finding a substring:

with arcpy.da.UpdateCursor("in_memory/headphotos",[field2,'Located']) as UCur1:
    for URow1 in UCur1:
        if URow1[0].lower().find('reserve') > 0:
            URow1[1] = 1           # this row is found to contain 'reserve'
            UCur1.updateRow(URow1) # store the updated row

So what's the problem with doing it this way? You're still going through all the rows again, this mightn't be a big deal in small datasets but it can be more significant in very large datasets where the power of some SQL to limit the number of rows that python has to chug through gives a significant advantage; SQL is usually faster than python.

On a side note, I see you've made the 2nd cursor UCur1 to avoid reusing the name, this often is commendable, however by the time the execution gets to the second update cursor UCur and URow from the first cursor are out of scope and therefore do not exist - as far as python is concerned it's already forgotten them! Don't feel that you need to have a different variable name for every cursor... I've used the name UCur/URow to remind me that it's an update environment (read/write), were it a search cursor I might use SCur/SRow to remind me it's a search environment (read only) but as soon as you finish the with block those names are safe to be reused.

Expanding on the comment.

There's two ways set your rows that contain 'reserve' location attribute to 1, but first I must point out your 2nd update cursor does not contain UCur1.updateRow(URow1) so no matter what no rows are updated.

Firstly by where clause:

with arcpy.da.UpdateCursor("in_memory/headphotos",[field2,'Located'],'{} like \'%reserve%\''.format(field2)) as UCur1:
    for URow1 in UCur1:
      URow1[1] = 1           # all rows contain the word 'reserve' in their field2.
      UCur1.updateRow(URow1) # store the updated row

Where can this be a problem? The wildcard character is different for different feature class types.. % is fine most of the time but personal geodatabases use * as a wildcard which can cause horrible problems when reusing code.

Secondly by finding a substring:

with arcpy.da.UpdateCursor("in_memory/headphotos",[field2,'Located']) as UCur1:
    for URow1 in UCur1:
        if URow1[0].lower().find('reserve') > 0:
            URow1[1] = 1           # this row is found to contain 'reserve'
            UCur1.updateRow(URow1) # store the updated row

So what's the problem with doing it this way? You're still going through all the rows again, this mightn't be a big deal in small datasets but it can be more significant in very large datasets where the power of some SQL to limit the number of rows that python has to chug through gives a significant advantage; SQL is usually faster than python.

Expanding on the comment.

There's two ways set your rows that contain 'reserve' location attribute to 1, but first I must point out your 2nd update cursor does not contain UCur1.updateRow(URow1) so no matter what no rows are updated.

Firstly by where clause:

with arcpy.da.UpdateCursor("in_memory/headphotos",[field2,'Located'],'{} like \'%reserve%\''.format(field2)) as UCur1:
    for URow1 in UCur1:
      URow1[1] = 1           # all rows contain the word 'reserve' in their field2.
      UCur1.updateRow(URow1) # store the updated row

Where can this be a problem? The wildcard character is different for different feature class types.. % is fine most of the time but personal geodatabases use * as a wildcard which can cause horrible problems when reusing code.

Secondly by finding a substring:

with arcpy.da.UpdateCursor("in_memory/headphotos",[field2,'Located']) as UCur1:
    for URow1 in UCur1:
        if URow1[0].lower().find('reserve') > 0:
            URow1[1] = 1           # this row is found to contain 'reserve'
            UCur1.updateRow(URow1) # store the updated row

So what's the problem with doing it this way? You're still going through all the rows again, this mightn't be a big deal in small datasets but it can be more significant in very large datasets where the power of some SQL to limit the number of rows that python has to chug through gives a significant advantage; SQL is usually faster than python.

On a side note, I see you've made the 2nd cursor UCur1 to avoid reusing the name, this often is commendable, however by the time the execution gets to the second update cursor UCur and URow from the first cursor are out of scope and therefore do not exist - as far as python is concerned it's already forgotten them! Don't feel that you need to have a different variable name for every cursor... I've used the name UCur/URow to remind me that it's an update environment (read/write), were it a search cursor I might use SCur/SRow to remind me it's a search environment (read only) but as soon as you finish the with block those names are safe to be reused.

Source Link
Michael Stimson
  • 25.7k
  • 2
  • 35
  • 75

Expanding on the comment.

There's two ways set your rows that contain 'reserve' location attribute to 1, but first I must point out your 2nd update cursor does not contain UCur1.updateRow(URow1) so no matter what no rows are updated.

Firstly by where clause:

with arcpy.da.UpdateCursor("in_memory/headphotos",[field2,'Located'],'{} like \'%reserve%\''.format(field2)) as UCur1:
    for URow1 in UCur1:
      URow1[1] = 1           # all rows contain the word 'reserve' in their field2.
      UCur1.updateRow(URow1) # store the updated row

Where can this be a problem? The wildcard character is different for different feature class types.. % is fine most of the time but personal geodatabases use * as a wildcard which can cause horrible problems when reusing code.

Secondly by finding a substring:

with arcpy.da.UpdateCursor("in_memory/headphotos",[field2,'Located']) as UCur1:
    for URow1 in UCur1:
        if URow1[0].lower().find('reserve') > 0:
            URow1[1] = 1           # this row is found to contain 'reserve'
            UCur1.updateRow(URow1) # store the updated row

So what's the problem with doing it this way? You're still going through all the rows again, this mightn't be a big deal in small datasets but it can be more significant in very large datasets where the power of some SQL to limit the number of rows that python has to chug through gives a significant advantage; SQL is usually faster than python.