<?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: Using SearchCursor and CalculateField in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-searchcursor-and-calculatefield/m-p/512736#M40292</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ben,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It might be easier in this case (and probably faster) to simply us a feature layer and your calculate field.&amp;nbsp; I would try something like this:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14065523395527279 jive_text_macro" jivemacro_uid="_14065523395527279"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;fc = r"c:\Test_Orient.shp"&amp;nbsp; &lt;/P&gt;
&lt;P&gt;feildName = "ORIENT"&amp;nbsp; &lt;/P&gt;
&lt;P&gt;expression = "rand_num()*360"&amp;nbsp; &lt;/P&gt;
&lt;P&gt;codeblock = """def rand_num(): &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; import random &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return random.random()"""&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;arcpy.MakeFeatureLayer_management(fc, "TEST_ORIENT_LAYER", fieldName + " = 0")&lt;/P&gt;
&lt;P&gt;arcpy.CalculateField_management("TEST_ORIENT_LAYER", fieldName, expression, "PYTHON_9.3", codeblock)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Using make feature layer makes an "in memory" pointer to the layer, much like adding the layer to an MXD does, it also happens to allow you to set a where clause.&amp;nbsp; Once you have this (I called it TEST_ORIENT_LAYER) then you can access it in almost any toolbox, as almost all of them take in a Feature Layer or a Feature Class (shape files included).&amp;nbsp; Check out the documentation here:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Make_Feature_Layer/00170000006p000000/" title="http://resources.arcgis.com/en/help/main/10.1/index.html#/Make_Feature_Layer/00170000006p000000/"&gt;ArcGIS Help 10.1&lt;/A&gt;‌&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As an aside, in my testing, until you want to calculate more than about 5 columns per row, it is faster to use a method like this as the Calc Field tool is faster than even a Data Access cursor until about 5 columns.&amp;nbsp; After 5 columns, you should probably open the cursor and do the calculations yourself.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 28 Jul 2014 13:03:00 GMT</pubDate>
    <dc:creator>ChristopherMoravec1</dc:creator>
    <dc:date>2014-07-28T13:03:00Z</dc:date>
    <item>
      <title>Using SearchCursor and CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-and-calculatefield/m-p/512735#M40291</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi - I'm trying to write a little bit of code to assign a random value to my ORIENT field where ORIENT = 0.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My problem is that it chages the ORIENT for the whole table not just where the value is zero. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I guess that this is because line 14 does not refer to row but I don't know how to charge it so it does refer to row - can anyone advise??&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14065455086973865" jivemacro_uid="_14065455086973865"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;fc = r"c:\Test_Orient.shp"&lt;/P&gt;
&lt;P&gt;feildName = "ORIENT"&lt;/P&gt;
&lt;P&gt;expression = "rand_num()*360"&lt;/P&gt;
&lt;P&gt;codeblock = """def rand_num():&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; import random&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return random.random()"""&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;#search for rows to be altered&lt;/P&gt;
&lt;P&gt;rows = arcpy.SearchCursor(fc, fieldName + " = 0")&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;for row in rows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(fc, fieldName, expression, "PYTHON_9.3", codeblock)&lt;/P&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jul 2014 11:19:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-and-calculatefield/m-p/512735#M40291</guid>
      <dc:creator>BenLeslie1</dc:creator>
      <dc:date>2014-07-28T11:19:26Z</dc:date>
    </item>
    <item>
      <title>Re: Using SearchCursor and CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-and-calculatefield/m-p/512736#M40292</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ben,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It might be easier in this case (and probably faster) to simply us a feature layer and your calculate field.&amp;nbsp; I would try something like this:&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14065523395527279 jive_text_macro" jivemacro_uid="_14065523395527279"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;fc = r"c:\Test_Orient.shp"&amp;nbsp; &lt;/P&gt;
&lt;P&gt;feildName = "ORIENT"&amp;nbsp; &lt;/P&gt;
&lt;P&gt;expression = "rand_num()*360"&amp;nbsp; &lt;/P&gt;
&lt;P&gt;codeblock = """def rand_num(): &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; import random &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return random.random()"""&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;arcpy.MakeFeatureLayer_management(fc, "TEST_ORIENT_LAYER", fieldName + " = 0")&lt;/P&gt;
&lt;P&gt;arcpy.CalculateField_management("TEST_ORIENT_LAYER", fieldName, expression, "PYTHON_9.3", codeblock)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Using make feature layer makes an "in memory" pointer to the layer, much like adding the layer to an MXD does, it also happens to allow you to set a where clause.&amp;nbsp; Once you have this (I called it TEST_ORIENT_LAYER) then you can access it in almost any toolbox, as almost all of them take in a Feature Layer or a Feature Class (shape files included).&amp;nbsp; Check out the documentation here:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Make_Feature_Layer/00170000006p000000/" title="http://resources.arcgis.com/en/help/main/10.1/index.html#/Make_Feature_Layer/00170000006p000000/"&gt;ArcGIS Help 10.1&lt;/A&gt;‌&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As an aside, in my testing, until you want to calculate more than about 5 columns per row, it is faster to use a method like this as the Calc Field tool is faster than even a Data Access cursor until about 5 columns.&amp;nbsp; After 5 columns, you should probably open the cursor and do the calculations yourself.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jul 2014 13:03:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-and-calculatefield/m-p/512736#M40292</guid>
      <dc:creator>ChristopherMoravec1</dc:creator>
      <dc:date>2014-07-28T13:03:00Z</dc:date>
    </item>
    <item>
      <title>Re: Using SearchCursor and CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-and-calculatefield/m-p/512737#M40293</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ben,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Rather than calculate the row you will benefit from generating an updateCursor object rather than using the CalculateField gp tool. I believe the data access module (arcpy.da) is more efficient. You can check out the links &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/UpdateCursor/018w00000014000000/"&gt;here&lt;/A&gt;, &lt;A href="http://http//resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000"&gt;here&lt;/A&gt; and &lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000009r000000"&gt;here&lt;/A&gt; for more information.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_1406552965563391" jivemacro_uid="_1406552965563391" modifiedtitle="true"&gt;
&lt;P&gt;Import arcpy, random&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;from arcpy import da&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;fc = r"c:\Test_Orient.shp"&lt;/P&gt;
&lt;P&gt;fieldName = "ORIENT"&lt;/P&gt;
&lt;P&gt;whereClause = fieldName + " = 0"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="comment" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #008200; background-color: #f8f8f8;"&gt;# create update cursor&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="comment" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #008200; background-color: #f8f8f8;"&gt;urows = arcpy.da.updateCursor(fc, whereClause)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="comment" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #008200; background-color: #f8f8f8;"&gt;for urow in urows:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="comment" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #008200; background-color: #f8f8f8;"&gt;&lt;CODE&gt;&lt;SPAN class="n"&gt;&lt;/SPAN&gt;&lt;SPAN class="p"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; u&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/SPAN&gt;row.setValue(fieldName, row.getValue(random.random() * 360)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; urows.updateRow(urow)&lt;/P&gt;


&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jul 2014 13:17:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-and-calculatefield/m-p/512737#M40293</guid>
      <dc:creator>JohnFell</dc:creator>
      <dc:date>2014-07-28T13:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Using SearchCursor and CalculateField</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-and-calculatefield/m-p/512738#M40294</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This one works for me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks John for your answer - looks pretty straight forward too - I will test it later as a learning exercise.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jul 2014 13:46:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-and-calculatefield/m-p/512738#M40294</guid>
      <dc:creator>BenLeslie1</dc:creator>
      <dc:date>2014-07-28T13:46:27Z</dc:date>
    </item>
  </channel>
</rss>

