for loop in a for loop... I'm stuck!

651
7
03-17-2014 07:27 AM
GobTron
New Contributor III
Hi! So I am new to python scripting, and after a few hours being stuck somewhere in my code, I require your help!

For some reason, the code goes in the 2nd loop only the first time. Then the first loop finishes without going into the second.
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).

What's wrong? I can't figure it out.


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:  
  data_to_get = row1.getValue("NEAR_FID")
  print data_to_get
  for row2 in cursor2:
    if row2.getValue("FID") == data_to_get:
      print row2.getValue("ph"), "loop2"
      cursor2.reset()
Tags (2)
0 Kudos
7 Replies
DuncanHornby
MVP Notable Contributor
I think the problem is nothing more than the indentation which controls the loops. I believe the code should be:


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: 
  data_to_get = row1.getValue("NEAR_FID")
  print data_to_get
  for row2 in cursor2:
    if row2.getValue("FID") == data_to_get:
      print row2.getValue("ph"), "loop2"
  cursor2.reset()
0 Kudos
GobTron
New Contributor III
I think the problem is nothing more than the indentation which controls the loops. I believe the code should be:


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: 
  data_to_get = row1.getValue("NEAR_FID")
  print data_to_get
  for row2 in cursor2:
    if row2.getValue("FID") == data_to_get:
      print row2.getValue("ph"), "loop2"
  cursor2.reset()



Nope... I still have the same problem.
0 Kudos
DuncanHornby
MVP Notable Contributor
Ok well an alternate logic would be:

import arcpy
in_feature = "3ans"
near_feature = "PhysicoChimie3ans"


cursor1 = arcpy.SearchCursor(in_feature, fields="NEAR_FID")


for row1 in cursor1:  
  data_to_get = row1.getValue("NEAR_FID")
  print data_to_get
  cursor2 = arcpy.SearchCursor(near_feature, fields="pH")
  for row2 in cursor2:
    if row2.getValue("FID") == data_to_get:
      print row2.getValue("ph"), "loop2"

  del row2,cursor2
0 Kudos
GobTron
New Contributor III
Yes! It is working!

I had an error in the line  
  del row2,cursor2 


So here is what I used.


import arcpy
in_feature = "3ans"
near_feature = "PhysicoChimie3ans"


cursor1 = arcpy.SearchCursor(in_feature, fields="NEAR_FID")


for row1 in cursor1:  
  data_to_get = row1.getValue("NEAR_FID")
  print data_to_get
  cursor2 = arcpy.SearchCursor(near_feature, fields="pH")
  for row2 in arcpy.SearchCursor(near_feature, fields="pH"):
    if row2.getValue("FID") == data_to_get:
      print row2.getValue("ph"), "loop2"

  del row2
0 Kudos
Zeke
by
Regular Contributor III
You can also use a with statement when initializing the cursor to eliminate the need for del:

with arcpy.SearchCursor(in_feature, fields="NEAR_FID")as cursor1:
    #loops here
0 Kudos
GobTron
New Contributor III
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.

So instead of printing data_to_write, I want to write it in the table of in_feature in the field "ph"

Confused...
0 Kudos
benberman
Occasional Contributor
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.

So instead of printing data_to_write, I want to write it in the table of in_feature in the field "ph"

Confused...


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...

something like

with open('path to text file.txt','w') as f:
     lst= [row.getvalue(field) for row in arcpy.searchcursor(in_feature, field)]
     for item in lst:
          f.write(item)
0 Kudos