<?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 for loop in a for loop... I'm stuck! in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134806#M10570</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi! So I am new to python scripting, and after a few hours being stuck somewhere in my code, I require your help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For some reason, the code goes in the 2nd loop only the first time. Then the first loop finishes without going into the second.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Loop 1 is supposed to read each row of the attribute table, ine the NEAR_FID field. Then, it reads a second attribute table and it does an action when NEAR_FID (from 1st table) == FID (from 2nd table).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What's wrong? I can't figure it out.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
in_feature = "3ans"
near_feature = "PhysicoChimie3ans"

cursor1 = arcpy.SearchCursor(in_feature, fields="NEAR_FID")
cursor2 = arcpy.SearchCursor(near_feature, fields="pH")
&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; 
for row1 in cursor1:&amp;nbsp; 
&amp;nbsp; data_to_get = row1.getValue("NEAR_FID")
&amp;nbsp; print data_to_get
&amp;nbsp; for row2 in cursor2:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row2.getValue("FID") == data_to_get:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row2.getValue("ph"), "loop2"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor2.reset()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 17 Mar 2014 14:27:51 GMT</pubDate>
    <dc:creator>GobTron</dc:creator>
    <dc:date>2014-03-17T14:27:51Z</dc:date>
    <item>
      <title>for loop in a for loop... I'm stuck!</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134806#M10570</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi! So I am new to python scripting, and after a few hours being stuck somewhere in my code, I require your help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For some reason, the code goes in the 2nd loop only the first time. Then the first loop finishes without going into the second.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Loop 1 is supposed to read each row of the attribute table, ine the NEAR_FID field. Then, it reads a second attribute table and it does an action when NEAR_FID (from 1st table) == FID (from 2nd table).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What's wrong? I can't figure it out.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
in_feature = "3ans"
near_feature = "PhysicoChimie3ans"

cursor1 = arcpy.SearchCursor(in_feature, fields="NEAR_FID")
cursor2 = arcpy.SearchCursor(near_feature, fields="pH")
&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; 
for row1 in cursor1:&amp;nbsp; 
&amp;nbsp; data_to_get = row1.getValue("NEAR_FID")
&amp;nbsp; print data_to_get
&amp;nbsp; for row2 in cursor2:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row2.getValue("FID") == data_to_get:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row2.getValue("ph"), "loop2"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor2.reset()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Mar 2014 14:27:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134806#M10570</guid>
      <dc:creator>GobTron</dc:creator>
      <dc:date>2014-03-17T14:27:51Z</dc:date>
    </item>
    <item>
      <title>Re: for loop in a for loop... I'm stuck!</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134807#M10571</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I think the problem is nothing more than the indentation which controls the loops. I believe the code should be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

in_feature = "3ans"
near_feature = "PhysicoChimie3ans"

cursor1 = arcpy.SearchCursor(in_feature, fields="NEAR_FID")
cursor2 = arcpy.SearchCursor(near_feature, fields="pH") 

for row1 in cursor1: 
&amp;nbsp; data_to_get = row1.getValue("NEAR_FID")
&amp;nbsp; print data_to_get
&amp;nbsp; for row2 in cursor2:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row2.getValue("FID") == data_to_get:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row2.getValue("ph"), "loop2"
&lt;SPAN style="color:#ff0000;"&gt;&lt;STRONG&gt;&amp;nbsp; cursor2.reset()
&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:32:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134807#M10571</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2021-12-11T07:32:29Z</dc:date>
    </item>
    <item>
      <title>Re: for loop in a for loop... I'm stuck!</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134808#M10572</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I think the problem is nothing more than the indentation which controls the loops. I believe the code should be:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

in_feature = "3ans"
near_feature = "PhysicoChimie3ans"

cursor1 = arcpy.SearchCursor(in_feature, fields="NEAR_FID")
cursor2 = arcpy.SearchCursor(near_feature, fields="pH") 

for row1 in cursor1: 
&amp;nbsp; data_to_get = row1.getValue("NEAR_FID")
&amp;nbsp; print data_to_get
&amp;nbsp; for row2 in cursor2:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row2.getValue("FID") == data_to_get:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row2.getValue("ph"), "loop2"
&lt;SPAN style="color:#ff0000;"&gt;&lt;STRONG&gt;&amp;nbsp; cursor2.reset()
&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Nope... I still have the same problem.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:32:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134808#M10572</guid>
      <dc:creator>GobTron</dc:creator>
      <dc:date>2021-12-11T07:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: for loop in a for loop... I'm stuck!</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134809#M10573</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok well an alternate logic would be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
in_feature = "3ans"
near_feature = "PhysicoChimie3ans"


cursor1 = arcpy.SearchCursor(in_feature, fields="NEAR_FID")


for row1 in cursor1:&amp;nbsp; 
&amp;nbsp; data_to_get = row1.getValue("NEAR_FID")
&amp;nbsp; print data_to_get
&amp;nbsp; cursor2 = arcpy.SearchCursor(near_feature, fields="pH")
&amp;nbsp; for row2 in cursor2:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row2.getValue("FID") == data_to_get:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row2.getValue("ph"), "loop2"

&amp;nbsp; del row2,cursor2
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:32:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134809#M10573</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2021-12-11T07:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: for loop in a for loop... I'm stuck!</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134810#M10574</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes! It is working! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I had an error in the line&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp; del row2,cursor2 &lt;/PRE&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So here is what I used.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
in_feature = "3ans"
near_feature = "PhysicoChimie3ans"


cursor1 = arcpy.SearchCursor(in_feature, fields="NEAR_FID")


for row1 in cursor1:&amp;nbsp; 
&amp;nbsp; data_to_get = row1.getValue("NEAR_FID")
&amp;nbsp; print data_to_get
&amp;nbsp; cursor2 = arcpy.SearchCursor(near_feature, fields="pH")
&amp;nbsp; for row2 in arcpy.SearchCursor(near_feature, fields="pH"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row2.getValue("FID") == data_to_get:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row2.getValue("ph"), "loop2"

&amp;nbsp; del row2
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:32:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134810#M10574</guid>
      <dc:creator>GobTron</dc:creator>
      <dc:date>2021-12-11T07:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: for loop in a for loop... I'm stuck!</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134811#M10575</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can also use a with statement when initializing the cursor to eliminate the need for del:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
with arcpy.SearchCursor(in_feature, fields="NEAR_FID")as cursor1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #loops here
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 07:32:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134811#M10575</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2021-12-11T07:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: for loop in a for loop... I'm stuck!</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134812#M10576</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Now the problem in that script is that I want to write the data extracted from the 2nd table in the 1st table (instead of just "printing" it"). I know I have to use an UpdateCursor but in don't understand how to use it properly when dealing with two tables instead of just one. Exemples I have seen are for taking a data in a field and writing something in another field inside the same table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So instead of printing data_to_write, I want to write it in the table of in_feature in the field "ph"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Confused...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Mar 2014 19:15:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134812#M10576</guid>
      <dc:creator>GobTron</dc:creator>
      <dc:date>2014-03-26T19:15:36Z</dc:date>
    </item>
    <item>
      <title>Re: for loop in a for loop... I'm stuck!</title>
      <link>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134813#M10577</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Now the problem in that script is that I want to write the data extracted from the 2nd table in the 1st table (instead of just "printing" it"). I know I have to use an UpdateCursor but in don't understand how to use it properly when dealing with two tables instead of just one. Exemples I have seen are for taking a data in a field and writing something in another field inside the same table.&lt;BR /&gt;&lt;BR /&gt;So instead of printing data_to_write, I want to write it in the table of in_feature in the field "ph"&lt;BR /&gt;&lt;BR /&gt;Confused...&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;If I am understanding correctly, you to do a searchcursor using in feature indexing it to field "ph" then you need to loop each row in the field...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;something like&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;with open('path to text file.txt','w') as f:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lst= [row.getvalue(field) for row in arcpy.searchcursor(in_feature, field)]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for item in lst:&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; f.write(item)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Mar 2014 13:30:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/for-loop-in-a-for-loop-i-m-stuck/m-p/134813#M10577</guid>
      <dc:creator>benberman</dc:creator>
      <dc:date>2014-03-31T13:30:06Z</dc:date>
    </item>
  </channel>
</rss>

