<?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: Better way to do an UpdateCursor on several fields?? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21430#M1668</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'd just use IF statements. Row objects have the fields as attributes, so you can use row.FIELD to clarify your code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inFeatures = os.getcwd() + '\\''myMDB.mdb\\myFC'

row = None
uc = arcpy.UpdateCursor(inFeatures):
for row in uc
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.STATUS == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.HAZ_STATUS = "No Violations"
#&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.OTHERFIELD == X:
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.HAZ_OTHER = 'Some Text'
&amp;nbsp;&amp;nbsp;&amp;nbsp; uc.updateRow(row)
del uc, row&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 20:52:32 GMT</pubDate>
    <dc:creator>JohnCobb1</dc:creator>
    <dc:date>2021-12-10T20:52:32Z</dc:date>
    <item>
      <title>Better way to do an UpdateCursor on several fields??</title>
      <link>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21429#M1667</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello - I am trying to query a field(type-double) called "STATUS" and where there are "0's in that field, I want to update another field called "HAZ_STATUS"(type-text). So this might not be the best method, not sure, but it works. My question is, if I had to do this for another 15 fields, would I have to insert this code 15 times, or is there a better method? Also, I would update "STATUS" directly, but the field type is "double", and I want to update it with "text". So creating a new field with a new field type is the only solution, right?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I appreciate any comments - I'm pretty new at this..&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;fldName6 = "HAZ_STATUS"
inFeatures = os.getcwd() + '\\''myMDB.mdb\\myFC'
query = '[STATUS] = 0'
rows = arcpy.UpdateCursor(inFeatures, query)
row = rows.next()
while row:
&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(fldName6, "No Violations")
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.UpdateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.next()
del row, rows&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Aug 2012 23:46:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21429#M1667</guid>
      <dc:creator>JoelThomas</dc:creator>
      <dc:date>2012-08-01T23:46:45Z</dc:date>
    </item>
    <item>
      <title>Re: Better way to do an UpdateCursor on several fields??</title>
      <link>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21430#M1668</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'd just use IF statements. Row objects have the fields as attributes, so you can use row.FIELD to clarify your code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inFeatures = os.getcwd() + '\\''myMDB.mdb\\myFC'

row = None
uc = arcpy.UpdateCursor(inFeatures):
for row in uc
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.STATUS == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.HAZ_STATUS = "No Violations"
#&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.OTHERFIELD == X:
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.HAZ_OTHER = 'Some Text'
&amp;nbsp;&amp;nbsp;&amp;nbsp; uc.updateRow(row)
del uc, row&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:52:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21430#M1668</guid>
      <dc:creator>JohnCobb1</dc:creator>
      <dc:date>2021-12-10T20:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: Better way to do an UpdateCursor on several fields??</title>
      <link>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21431#M1669</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You should use a query in the cursor definition only to resrict the rows in the cursor. Which ideed seems to be what you want to do in the situation that you report works.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What exactly you mean by "do this for another 15 fields", however, is not clear.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Do you want to set another 15 field values based on the the fact that [STATUS] = 0?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Or do you want to test another 15 fields beside [STATUS]?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If the first, you can just add lines setting the value of each field to the existing setValue,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;then fire the updateRow.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If, as seems likely, you want the second option (to test 15 fields, and set some value accordingly), then:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Run the cursor on the whole table (no query)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;test each of the items of interest in each row; if the condition is met, then set the value.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;rows = arcpy.UpdateCursor(inFeatures)
row = rows.next()
while row:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.STATUS == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(fldName6, "No Violations")
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.OtherField == 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(fldName7, "Something")
#&amp;nbsp;&amp;nbsp; other test here...
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.UpdateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.next()
 &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note that for Arc10 you should use&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for row in rows:&lt;/PRE&gt;&lt;SPAN&gt;intead of &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;row = rows.next()
while row:
&amp;nbsp;&amp;nbsp;&amp;nbsp; ....
&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.next()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;... and yes you need a text item to place text. One can place a number in a text field (and turn it back into a number when you take it out), but text just will not go in a numeric field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Although you CAN put the ansi or ascii code value of a text character in a numeric field, but the question then becomes: why?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:52:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21431#M1669</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2021-12-10T20:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: Better way to do an UpdateCursor on several fields??</title>
      <link>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21432#M1670</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;i think the part you'd need to repeat 15x would be&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;row.setValue(fldName6, "No Violations")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;but for each field and updated information.&amp;nbsp; So you'd potentially have (fldName7,"New Value").. 15 lines of code inside your while loop if you were to do this outside of a loop.&amp;nbsp; If you're doing the same thing for each field (i.e. converting 0 values to some sort of text) you could create a list of the fields then iterate through those for each row that you're on. so something like the following might work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;rows = arcpy.UpdateCursor(inFeatures, query,'field_a;field_b;field_c... field_x')
field_list = ['field_a','field_b','field_c'...'field_x']
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in field_list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; field_x = 'row.' + field
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(field_x, "No Violations")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.UpdateRow(row)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This sort of thing gets more complicated of course depending on the number of different scenarios you have to handle, but you could work out a variety of if/else type statements to handle that sort of decision making.&amp;nbsp; And yes, if the field is currently a double but you need a text value you need to create a new text field to capture that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One thing I'm not sure about in your code is the use of the while loop... maybe its a bit of preference but a 'for loop' might be more efficent, and it keeps you from having to call row.next so it simplifies your code a little bit.&amp;nbsp; My understanding is that 'while loops' are good for checking on whether a particular condition has changed, which isn't really what you're doing in this example.&amp;nbsp; The for loop simply steps through each row in your cursor, performing the actions you want on each row, then moving to the next row and repeating those actions..&amp;nbsp; until it reaches the end.&amp;nbsp; Ultimately, I'm not sure this is a significant issue - your code obviously is working, but might be something to consider, and a lot of this may depend on how many rows you are dealing with.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:52:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21432#M1670</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2021-12-10T20:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: Better way to do an UpdateCursor on several fields??</title>
      <link>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21433#M1671</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the 3 excellent responses. I was using the while loop when I was testing this in 9.3 - I didn't plan on changing it, but now I will that I can see the difference..thanks again!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Aug 2012 14:06:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/better-way-to-do-an-updatecursor-on-several-fields/m-p/21433#M1671</guid>
      <dc:creator>JoelThomas</dc:creator>
      <dc:date>2012-08-02T14:06:45Z</dc:date>
    </item>
  </channel>
</rss>

