<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Problem with Update Cursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62345#M5013</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;These two rows&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; del rows&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; arcpy.AddMessage("Completed")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;need to be de-dented to line up with&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the for statement&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.UpdateCursor(infc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: # iterate over the rows
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Completed")
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 22:23:01 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2021-12-10T22:23:01Z</dc:date>
    <item>
      <title>Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62337#M5005</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Im new to Python and have been trying to teach myself. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to calculate fields based on the results of two fields and read that using an update cursor is the best way to do this. I have attemped to create one but it still doesn't work, its not returning any errors but its doens't return any results. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Help!! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;#--------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import sys&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcgisscripting&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import functools&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy.geoprocessing&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def updateCursor(str):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; """Update fields based on analyis"""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.gp._base._updateCursor = True&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.overwriteOutput = True&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace = "C:\Temp\BAL_Mapping.gdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create Update Cursor for feature Class&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.gp._base._updateCursor ("C:\Temp\BAL_Mapping.gdb\INTERFACE")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = row.next ()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while row:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Slope == "0-5" and row.Veg == "Woodland":&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.BALFZ = "15"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif row.Slope == "0-5" and row.Veg == "Forest":&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.BALFZ = "24"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.BALFZ = "0"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.row (row)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = row.next ()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; except arcpy.ExecuteError:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages(2)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print e.args[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# End updateCursor function&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;if __name__ == '__main__':&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; argv = tuple(arcpy.GetParameterAsText(i)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for i in range(arcpy.GetArgumentCount()))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; updateCursor(str)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Sep 2011 00:33:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62337#M5005</guid>
      <dc:creator>DeidreEllis</dc:creator>
      <dc:date>2011-09-20T00:33:21Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62338#M5006</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;good work having a crack at this; unfortunately your script seems a little more complicated than necessary (I guess you have been reading some Python books, or are used to programming in something like C++). A good place to start for these kind of things is the ESRI documentation for Arcpy; i.e. &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000003m000000" rel="nofollow noopener noreferrer" target="_blank"&gt;UpdateCursor&lt;/A&gt;&lt;SPAN&gt; (whenever I get stuck I just google "arcpy thing" for whatever thing I am having trouble with).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just a few notes on your code&lt;/SPAN&gt;&lt;BR /&gt;&lt;OL&gt;&lt;BR /&gt;&lt;LI&gt;don't import anything you don't need - this will just slow things down unnecessarily&lt;/LI&gt;&lt;BR /&gt;&lt;LI&gt;an operation like this is simple enough that you can just make it a script (see below); you don't need to start defining functions&lt;/LI&gt;&lt;BR /&gt;&lt;/OL&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is a script to do what you want, assuming you specify just one parameter, which is the feature class you want to do the operation on:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

featureClass = arcpy.GetParameterAsText(0) # if running from Arc as a script tool, get first (0th) parameter
if len(featureClass) == 0: # if not, it comes out as '' - has length 0; use this path
 featureClass = "C:\\Temp\\BAL_Mapping.gdb\\INTERFACE" # paths in python need either double slashes or forward slashed

arcpy.AddMessage('Starting operation on: %s' % featureClass) # tell user operation is starting

rows = arcpy.UpdateCursor(featureClass) # create the updatecursor rows object
for row in rows: # iterate over the rows
 if row.Slope == "0-5" and row.Veg == "Woodland":
&amp;nbsp; row.BALFZ = "15"
 elif row.Slope == "0-5" and row.Veg == "Forest":
&amp;nbsp; row.BALFZ = "24"
 else:
&amp;nbsp; row.BALFZ = "0"
&amp;nbsp; 
 rows.updateRow(row) # write the new value to the object

del row, rows

arcpy.AddMessage('Completed operation on: %s' % featureClass) # inform user of completion status&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Because this is so simple, and not really scalable in any meaningful way, I have changed it to be a script, but in my next post I will show you an example with a function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One other thing, which I didn't include above to keep it simple, is that row.GetValue() and row.SetValue() are more flexible operators to use for getting and setting attributes (see next post).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:22:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62338#M5006</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-10T22:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62339#M5007</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here is an updated version that uses a function (so you could call it from another script, or use it twice within the same script, etc.). Field names are hard coded here, but you could make them parameters from Arc.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To make it truly reusable you could make your conditional statements inputs to the function (as long as they followed the same format), but that is a bit more work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

def customUpdateCursor(FeatureClass, inField1, inField2, outField):
 '''customUpdateCursor(FeatureClass, inField1, inField2, outField)
 
 takes a feature class and three field names; two for input fields to the calculation and one for output
 
 performs a conditional assesment on the inputs and returns a value to the output 
 '''
 
 rows = arcpy.UpdateCursor(FeatureClass)
 for row in rows: # iterate over the rows
&amp;nbsp; if (row.getValue(inField1) == "0-5") and (row.getValue(inField2) == "Woodland"):
&amp;nbsp;&amp;nbsp; row.setValue(outField, "15")
&amp;nbsp; elif (row.getValue(inField1) == "0-5") and (row.getValue(inField2) == "Forest"):
&amp;nbsp;&amp;nbsp; row.setValue(outField, "24")
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; row.setValue(outField, "0")

&amp;nbsp; rows.updateRow(row)

 del row, rows
 
 # End of customUpdateCursor function
 

if __name__ == '__main__':
 featureClass = arcpy.GetParameterAsText(0) # if running from Arc as a script tool, get first (0th) parameter
 if len(featureClass) == 0: # if not, it comes out as '' - has length 0; use this default path
&amp;nbsp; featureClass =&amp;nbsp; "C:\\Temp\\BAL_Mapping.gdb\\INTERFACE" # paths in python need either double slashes or forward slashed
 
 arcpy.AddMessage('Starting operation on: %s' % featureClass)
 
 infield1 = 'Slope' # you could also make these input paramaters
 infield2 = 'Veg'
 outfield = 'BALFZ'
 
 result = customUpdateCursor(featureClass, infield1, infield2, outfield)# pass variables to function

 arcpy.AddMessage('Completed operation on: %s' % featureClass)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:22:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62339#M5007</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-10T22:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62340#M5008</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I just happened to need the exact same thing as this post. However what is the if__name etc part for? When I use just this part (with my input/output fields set as parameters)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;def customUpdateCursor(FeatureClass, inField1, inField2, outField):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; '''customUpdateCursor(FeatureClass, inField1, inField2, outField)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; takes a feature class and three field names; two for input fields to the calculation and one for output&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; performs a conditional assesment on the inputs and returns a value to the output &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; '''&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; rows = arcpy.UpdateCursor(FeatureClass)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; for row in rows: # iterate over the rows&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; if (row.getValue(inField1) == "Sandy") and (row.getValue(inField2) == "0-10"):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; row.setValue(outField, "poor")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; elif (row.getValue(inField1) == "Sandy") and (row.getValue(inField2) == "&amp;gt;10"):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; row.setValue(outField, "medium")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; else:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; row.setValue(outField, "none")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; rows.updateRow(row)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; del row, rows&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; # End of customUpdateCursor function&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;it says completed, but it doesn't update my "Richness" field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any idea? Do i need a searchcurser too?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Sep 2011 11:15:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62340#M5008</guid>
      <dc:creator>SylviaNiderla</dc:creator>
      <dc:date>2011-09-23T11:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62341#M5009</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;OK, I'll try to explain fully - sorry if you already know some of this stuff...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Essentially you can use Python to code in two ways. The first is just plain scripting, i.e.:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;do &lt;STRONG&gt;this&lt;/STRONG&gt;
if &lt;STRONG&gt;that&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; then do &lt;STRONG&gt;this&lt;/STRONG&gt;
else
&amp;nbsp;&amp;nbsp;&amp;nbsp; do &lt;STRONG&gt;this&lt;/STRONG&gt;
then do &lt;STRONG&gt;this&lt;/STRONG&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;The code starts at the top of the file, then goes through line by line, does whatever it is told to do then proceeds to the next line. My first post the other day contained a script. It got the input from the user (or defaulted to something), did the calculation, then told the user it was done.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;More advanced Python programming makes use of &lt;/SPAN&gt;&lt;STRONG style="font-style: italic;"&gt;Modules&lt;/STRONG&gt;&lt;SPAN&gt; (anything following a &lt;/SPAN&gt;&lt;STRONG&gt;def&lt;/STRONG&gt;&lt;SPAN&gt; statement is a module). Modules are sometimes also called &lt;/SPAN&gt;&lt;STRONG&gt;functions&lt;/STRONG&gt;&lt;SPAN&gt;. These allow you to use the same bit of code more than once. For example, if I have a code that selects by attributes, performs a buffer then exports the data, and I want to do it three times while varying a few things, I could do (just making stuff up to explain the example, this code will definitely not run):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inGDB = 'C:\data.gdb'
inFeatureClass = 'input'

# select field = 3, buffer by 5m, export to test_[fieldVal]_[buffersize] Featureclass in input GDB
selection = arcpy.selectByAttributes(inGDB + '\\' + inFeatureClass, 'field = 3')
bufferedSelection = arcpy.Buffer(selection,'5m')
arcpy.Export(bufferedSelection, inGDB + '\\' + 'test_3_5m')

# select field = 7, buffer by 7m, export to test_[fieldVal]_[buffersize] Featureclass in input GDB
selection = arcpy.selectByAttributes(inGDB + '\\' + inFeatureClass, 'field = 7')
bufferedSelection = arcpy.Buffer(selection,'7m')
arcpy.Export(bufferedSelection, inGDB + '\\' + 'test_7_7m')

# select field = 9, buffer by 1m, export to test_[fieldVal]_[buffersize] Featureclass in input GDB
selection = arcpy.selectByAttributes(inGDB + '\\' + inFeatureClass, 'field = 9')
bufferedSelection = arcpy.Buffer(selection,'1m')
arcpy.Export(bufferedSelection, inGDB + '\\' + 'test_9_1m') &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now, as I am repeating a lot of code, it would be simpler to define a module that takes the things that actually change as inputs and then does the operations. This module must be placed before it is called, so that it is loaded into memory. Adding the module gives:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# inputs
inGDB = 'C:\data.gdb'
inFeatureClass = 'input'

# define module (before trying to use it)
def selectBufferExport(GDB, FC, fieldName fieldVal, buffersize):
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''selects FC (within GDB) entries for fieldName == fieldVal
&amp;nbsp;&amp;nbsp;&amp;nbsp; buffers by buffersize
&amp;nbsp;&amp;nbsp;&amp;nbsp; exports to new featureclass in GDB with name "test_fieldVal_buffersize"
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''
&amp;nbsp;&amp;nbsp;&amp;nbsp; selection = arcpy.selectByAttributes(GDB + '\\' + FC, '%s = %s' % (fieldName, fieldVal))
&amp;nbsp;&amp;nbsp;&amp;nbsp; bufferedSelection = arcpy.Buffer(selection, buffersize)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Export(bufferedSelection, GDB + '\\' + 'test_%s_%s' % (fieldVal, buffersize))

# now call the module, passing it the variables
selectBufferExport(inGDB, inFeatureClass, 3, '5m')
selectBufferExport(inGDB, inFeatureClass, 7, '7m')
selectBufferExport(inGDB, inFeatureClass, 9, '1m')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# or, getting fancy, replacing the last four lines with a for loop:
for (fieldval,buffer) in [(3, '5m'), (7, '7m'), (9, '1m')]:
&amp;nbsp;&amp;nbsp;&amp;nbsp; selectBufferExport(inGDB, inFeatureClass, fieldVal, buffer)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Not only have we saved a bunch of lines, but if we want to make changes to the method or add a step, we only have to change it in one place.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now that I have created this module I can use it as above, &lt;/SPAN&gt;&lt;STRONG style="font-style: italic;"&gt;or &lt;/STRONG&gt;&lt;SPAN&gt;I can call it from other scripts by importing the *.py file it lives in; if it was saved as selBuffExp.py I could import it into another script within the same folder and call the module with:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import selBuffExp

selBuffExp.selectBufferExport('test.gdb', 'test', 3, '5m')&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Now, when you import a file it actually runs/loads into memory the contents of the file. &lt;/SPAN&gt;&lt;STRONG&gt;if __name__ == "__main__":&lt;/STRONG&gt;&lt;SPAN&gt; is a statement that only runs if you are running the script by itself (i.e. from the command line, or as an Arc tool). However, when the file is imported its name is not "__main__", and that bit doesn't get run... If you don't have it, and you try to import it, it will load the module into memory &lt;/SPAN&gt;&lt;STRONG style="font-style: italic;"&gt;and&lt;/STRONG&gt;&lt;SPAN&gt; do all the things within the script (which at least makes a mess, but probably crashes if your paths aren't still valid or both are set to the same paths).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Presumably you are making a script tool (not using it in a Field Calculator)? If you are making a script tool, the reason your code doesn't run is that you don't have the contents of the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;if __name__ == "__main__":&lt;/SPAN&gt;&lt;SPAN&gt; part, so the module is never actually called. Arc tells you it is complete, meaning that the tool completed, but all you have given it to do is load something into memory (you haven't called the module or anything)...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I modified the code from my first post to follow this convention as the original poster had used it; chances are you don't need to bother about it. If you aren't going to be importing it, you could just delete the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;if __name__ == "__main__":&lt;/SPAN&gt;&lt;SPAN&gt; line and un-indent what was in there.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:22:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62341#M5009</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-10T22:22:51Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62342#M5010</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;OK, I'll try to explain fully - sorry if you already know some of this stuff...&lt;BR /&gt;&lt;BR /&gt;Essentially you can use Python to code in two ways. The first is just plain scripting, i.e.:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;do &lt;STRONG&gt;this&lt;/STRONG&gt;
if &lt;STRONG&gt;that&lt;/STRONG&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; then do &lt;STRONG&gt;this&lt;/STRONG&gt;
else
&amp;nbsp;&amp;nbsp;&amp;nbsp; do &lt;STRONG&gt;this&lt;/STRONG&gt;
then do &lt;STRONG&gt;this&lt;/STRONG&gt;&lt;/PRE&gt;&lt;BR /&gt;The code starts at the top of the file, then goes through line by line, does whatever it is told to do then proceeds to the next line. My first post the other day contained a script. It got the input from the user (or defaulted to something), did the calculation, then told the user it was done.&lt;BR /&gt;&lt;BR /&gt;More advanced Python programming makes use of &lt;STRONG style="font-style: italic;"&gt;Modules&lt;/STRONG&gt; (anything following a &lt;STRONG&gt;def&lt;/STRONG&gt; statement is a module). Modules are sometimes also called &lt;STRONG&gt;functions&lt;/STRONG&gt;. These allow you to use the same bit of code more than once. For example, if I have a code that selects by attributes, performs a buffer then exports the data, and I want to do it three times while varying a few things, I could do (just making stuff up to explain the example, this code will definitely not run):&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inGDB = 'C:\data.gdb'
inFeatureClass = 'input'

# select field = 3, buffer by 5m, export to test_[fieldVal]_[buffersize] Featureclass in input GDB
selection = arcpy.selectByAttributes(inGDB + '\\' + inFeatureClass, 'field = 3')
bufferedSelection = arcpy.Buffer(selection,'5m')
arcpy.Export(bufferedSelection, inGDB + '\\' + 'test_3_5m')

# select field = 7, buffer by 7m, export to test_[fieldVal]_[buffersize] Featureclass in input GDB
selection = arcpy.selectByAttributes(inGDB + '\\' + inFeatureClass, 'field = 7')
bufferedSelection = arcpy.Buffer(selection,'7m')
arcpy.Export(bufferedSelection, inGDB + '\\' + 'test_7_7m')

# select field = 9, buffer by 1m, export to test_[fieldVal]_[buffersize] Featureclass in input GDB
selection = arcpy.selectByAttributes(inGDB + '\\' + inFeatureClass, 'field = 9')
bufferedSelection = arcpy.Buffer(selection,'1m')
arcpy.Export(bufferedSelection, inGDB + '\\' + 'test_9_1m') &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Now, as I am repeating a lot of code, it would be simpler to define a module that takes the things that actually change as inputs and then does the operations. This module must be placed before it is called, so that it is loaded into memory. Adding the module gives:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# inputs
inGDB = 'C:\data.gdb'
inFeatureClass = 'input'

# define module (before trying to use it)
def selectBufferExport(GDB, FC, fieldName fieldVal, buffersize):
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''selects FC (within GDB) entries for fieldName == fieldVal
&amp;nbsp;&amp;nbsp;&amp;nbsp; buffers by buffersize
&amp;nbsp;&amp;nbsp;&amp;nbsp; exports to new featureclass in GDB with name "test_fieldVal_buffersize"
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''
&amp;nbsp;&amp;nbsp;&amp;nbsp; selection = arcpy.selectByAttributes(GDB + '\\' + FC, '%s = %s' % (fieldName, fieldVal))
&amp;nbsp;&amp;nbsp;&amp;nbsp; bufferedSelection = arcpy.Buffer(selection, buffersize)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Export(bufferedSelection, GDB + '\\' + 'test_%s_%s' % (fieldVal, buffersize))

# now call the module, passing it the variables
selectBufferExport(inGDB, inFeatureClass, 3, '5m')
selectBufferExport(inGDB, inFeatureClass, 7, '7m')
selectBufferExport(inGDB, inFeatureClass, 9, '1m')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# or, getting fancy, replacing the last four lines with a for loop:
for (fieldval,buffer) in [(3, '5m'), (7, '7m'), (9, '1m')]:
&amp;nbsp;&amp;nbsp;&amp;nbsp; selectBufferExport(inGDB, inFeatureClass, fieldVal, buffer)&lt;/PRE&gt;&lt;BR /&gt;Not only have we saved a bunch of lines, but if we want to make changes to the method or add a step, we only have to change it in one place.&lt;BR /&gt;&lt;BR /&gt;Now that I have created this module I can use it as above, &lt;STRONG style="font-style: italic;"&gt;or &lt;/STRONG&gt;I can call it from other scripts by importing the *.py file it lives in; if it was saved as selBuffExp.py I could import it into another script within the same folder and call the module with:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import selBuffExp

selBuffExp.selectBufferExport('test.gdb', 'test', 3, '5m')&lt;/PRE&gt;&lt;BR /&gt;Now, when you import a file it actually runs/loads into memory the contents of the file. &lt;STRONG&gt;if __name__ == "__main__":&lt;/STRONG&gt; is a statement that only runs if you are running the script by itself (i.e. from the command line, or as an Arc tool). However, when the file is imported its name is not "__main__", and that bit doesn't get run... If you don't have it, and you try to import it, it will load the module into memory &lt;STRONG style="font-style: italic;"&gt;and&lt;/STRONG&gt; do all the things within the script (which at least makes a mess, but probably crashes if your paths aren't still valid or both are set to the same paths).&lt;BR /&gt;&lt;BR /&gt;Presumably you are making a script tool (not using it in a Field Calculator)? If you are making a script tool, the reason your code doesn't run is that you don't have the contents of the &lt;SPAN style="font-style:italic;"&gt;if __name__ == "__main__":&lt;/SPAN&gt; part, so the module is never actually called. Arc tells you it is complete, meaning that the tool completed, but all you have given it to do is load something into memory (you haven't called the module or anything)...&lt;BR /&gt;&lt;BR /&gt;I modified the code from my first post to follow this convention as the original poster had used it; chances are you don't need to bother about it. If you aren't going to be importing it, you could just delete the &lt;SPAN style="font-style:italic;"&gt;if __name__ == "__main__":&lt;/SPAN&gt; line and un-indent what was in there.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I meant to reply I get an error if I include the "main" etc bit. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;type 'exceptions.UnboundLocalError'&amp;gt;: local variable 'row' referenced before assignment&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (Script).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I just can't work it out. I've copied everything and checked it in pythonwin using script checker.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:22:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62342#M5010</guid>
      <dc:creator>SylviaNiderla</dc:creator>
      <dc:date>2021-12-10T22:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62343#M5011</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try replacing the line&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;del row, rows&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;With:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try: del row
except: pass
del rows&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think sometimes the row disappears before you get to delete it, something to do with how the cursor works (?), anyway, you are trying to delete something that doesn't exist, so it has an error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The reason you have no the problem when you delete the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;if __name__ == "__main__":&lt;/SPAN&gt;&lt;SPAN&gt; bit is that nothing actually happens - it doesn't run...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:22:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62343#M5011</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-10T22:22:56Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62344#M5012</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;well I just couldn't get it to work, so I tried writing it this way. But this way nothing happens also even though it runs fine. Any ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import sys, arcpy

try:
 infc = arcpy.GetParameterAsText(0)&amp;nbsp;&amp;nbsp; # Input feature class
 inField1 = arcpy.GetParameterAsText(1)&amp;nbsp; # Input data field
 inField2 = arcpy.GetParameterAsText(2)
 outfld = arcpy.GetParameterAsText(3)&amp;nbsp; # Output result field

except: 
 arcpy.AddError("Cannot parse input arguments")
 sys.exit("Error reading arguments!")
 arcpy.AddMessage("Calculating field...")&amp;nbsp;&amp;nbsp; 

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.UpdateCursor(infc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: # iterate over the rows
&amp;nbsp; if (row.getValue(inField1) == "Sandy") and (row.getValue(inField2) == "0-10"):
&amp;nbsp;&amp;nbsp; row.setValue(outField, "poor")
&amp;nbsp; elif (row.getValue(inField1) == "Sandy") and (row.getValue(inField2) == "&amp;gt;10"):
&amp;nbsp;&amp;nbsp; row.setValue(outField, "medium")
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; row.setValue(outField, "none")
&amp;nbsp; rows.updateRow(row)
&amp;nbsp; del rows
&amp;nbsp; arcpy.AddMessage("Completed")
except:
 errMsg = arcpy.GetMessages(2)
 arcpy.AddError("Unexpected error : " + errMsg)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:22:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62344#M5012</guid>
      <dc:creator>SylviaNiderla</dc:creator>
      <dc:date>2021-12-10T22:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62345#M5013</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;These two rows&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; del rows&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; arcpy.AddMessage("Completed")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;need to be de-dented to line up with&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the for statement&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.UpdateCursor(infc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: # iterate over the rows
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ....
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Completed")
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:23:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62345#M5013</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-10T22:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62346#M5014</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;ah, thanks. But now I just get an "unexpected error" so something in my "try" isn't right I guess.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ok I know its the searchcurser bit&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; rows = arcpy.SearchCursor(infc)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; for row in rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; infc = row.getValue(inField1) + row.getValue(inField2) &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; del rows&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;how do I get my curser to read over the two fields I want to use in the next bit?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Sep 2011 11:26:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62346#M5014</guid>
      <dc:creator>SylviaNiderla</dc:creator>
      <dc:date>2011-09-25T11:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62347#M5015</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If both fields aren't string/text, you can't concatenate them (ie use the + ) unless you convert them to string first ( ie using python's str method)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Sep 2011 19:17:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62347#M5015</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2011-09-25T19:17:33Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62348#M5016</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;ok, apparently I don't need the searchupdate curser bit. Anyway, I just want it to work for one field atm and then I will work out if it can work with numbers. But even that won't work, its something to do with the if/then statement, because I can copy one field to the other using the following code&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;rows = arcpy.UpdateCursor(infc)
 for row in rows:
&amp;nbsp; val = row.getValue (datafld)
&amp;nbsp; row.setValue(outfld,val)
&amp;nbsp; rows.updateRow(row)
 del rows&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;but when I start using if/else statements It errors.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.UpdateCursor(infc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (row.getValue(inField1) == "Sandy"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(outField, "poor")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(outField, "none")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Completed")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:23:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62348#M5016</guid>
      <dc:creator>SylviaNiderla</dc:creator>
      <dc:date>2021-12-10T22:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62349#M5017</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;your last two lines are indented incorrectly, you delete the rows in the first pass&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Sep 2011 08:13:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62349#M5017</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2011-09-27T08:13:28Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62350#M5018</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok I have it now by NAMING the fields. Also it wouldn't work with the brackets so it works like this&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;PRE&gt;&lt;/PRE&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.UpdateCursor(infc) &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue("Texture") == "Sandy" and row.getValue ("Humus") &amp;lt;= "10": #1-10 also doest not work&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("Richness", "poor")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("Richness", "none")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows, row&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Completed")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;*edit - nm I got it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Sep 2011 01:45:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62350#M5018</guid>
      <dc:creator>SylviaNiderla</dc:creator>
      <dc:date>2011-09-28T01:45:24Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62351#M5019</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Ok I have it now by NAMING the fields. Also it wouldn't work with the brackets so it works like this&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.UpdateCursor(infc) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.getValue("Texture") == "Sandy" and row.getValue ("Humus") &amp;lt;= "10": #1-10 also doest not work
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("Richness", "poor")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue("Richness", "none")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows, row
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Completed")

*edit - nm I got it.&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;By&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt; "#1-10 also doest not work"&lt;/SPAN&gt;&lt;SPAN&gt; do you mean you want it to be true if the contents of the field Humus is in the range of numbers 1...10? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Because the value of Humus is in a string, you can't test it with stuff like less than. You can either convert it to an number (floating point number), like so:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if (row.getValue("Texture") == "Sandy") and (float(row.getValue("Humus")) &amp;lt;= 10):&lt;/PRE&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Or you can specify a list of string values that it can be, and ask if the value is in that list, like so:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if (row.getValue("Texture") == "Sandy") and (row.getValue("Humus") in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']):&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:23:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62351#M5019</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-10T22:23:07Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62352#M5020</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you all it worked a treat!!! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was definatly making it more complicated than it needed to be. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you once again &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Deidre&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Oct 2011 23:40:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62352#M5020</guid>
      <dc:creator>DeidreEllis</dc:creator>
      <dc:date>2011-10-04T23:40:03Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62353#M5021</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I actually have 5 out fields, so would I just list them outField1, outField2, etc? &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I originally just wanted to get the script to work. Now that it does (thanks to you) I can complete it fully. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Here is an updated version that uses a function (so you could call it from another script, or use it twice within the same script, etc.). Field names are hard coded here, but you could make them parameters from Arc.&lt;BR /&gt;&lt;BR /&gt;To make it truly reusable you could make your conditional statements inputs to the function (as long as they followed the same format), but that is a bit more work.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

def customUpdateCursor(FeatureClass, inField1, inField2, outField):
 '''customUpdateCursor(FeatureClass, inField1, inField2, outField)
 
 takes a feature class and three field names; two for input fields to the calculation and one for output
 
 performs a conditional assesment on the inputs and returns a value to the output 
 '''
 
 rows = arcpy.UpdateCursor(FeatureClass)
 for row in rows: # iterate over the rows
&amp;nbsp; if (row.getValue(inField1) == "0-5") and (row.getValue(inField2) == "Woodland"):
&amp;nbsp;&amp;nbsp; row.setValue(outField, "15")
&amp;nbsp; elif (row.getValue(inField1) == "0-5") and (row.getValue(inField2) == "Forest"):
&amp;nbsp;&amp;nbsp; row.setValue(outField, "24")
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; row.setValue(outField, "0")

&amp;nbsp; rows.updateRow(row)

 del row, rows
 
 # End of customUpdateCursor function
 

if __name__ == '__main__':
 featureClass = arcpy.GetParameterAsText(0) # if running from Arc as a script tool, get first (0th) parameter
 if len(featureClass) == 0: # if not, it comes out as '' - has length 0; use this default path
&amp;nbsp; featureClass =&amp;nbsp; "C:\\Temp\\BAL_Mapping.gdb\\INTERFACE" # paths in python need either double slashes or forward slashed
 
 arcpy.AddMessage('Starting operation on: %s' % featureClass)
 
 infield1 = 'Slope' # you could also make these input paramaters
 infield2 = 'Veg'
 outfield = 'BALFZ'
 
 result = customUpdateCursor(featureClass, infield1, infield2, outfield)# pass variables to function

 arcpy.AddMessage('Completed operation on: %s' % featureClass)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:23:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62353#M5021</guid>
      <dc:creator>DeidreEllis</dc:creator>
      <dc:date>2021-12-10T22:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Update Cursor</title>
      <link>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62354#M5022</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You need to specify each of the fields when you define the module and when you call it. You then put each on a different line when you set the field values.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 Oct 2011 15:48:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-with-update-cursor/m-p/62354#M5022</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2011-10-07T15:48:19Z</dc:date>
    </item>
  </channel>
</rss>

