I want to use an if else statement to pass a condition

1373
11
Jump to solution
08-28-2012 05:27 PM
OLANIYANOLAKUNLE
Occasional Contributor II
I want to use an if else statement to pass a condition and it's not returning any of the two values what im i doing wrong?

fc = "Parcels" field = "GeneratedN" cursor = arcpy.SearchCursor(fc) for row in cursor:     row.getValue(field) Val = row.getValue(field) if Val == 1:     print "Done" else:     print "Not Done" 


Any suggestions please
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
I want to use an if else statement to pass a condition and it's not returning any of the two values what im i doing wrong?

fc = "Parcels" field = "GeneratedN" cursor = arcpy.SearchCursor(fc) for row in cursor:     row.getValue(field) Val = row.getValue(field) if Val == 1:     print "Done" else:     print "Not Done" 


Any suggestions please


Your indentation is wrong for the Val = row.getValue(field) line.  It occurs only after every record is read and should produce an error.  It needs to replace the row.getValue(field) line at the same indentation level.

fc = "Parcels" field = "GeneratedN" cursor = arcpy.SearchCursor(fc) for row in cursor:     Val = row.getValue(field) if Val == 1:     print "Done" else:     print "Not Done" 


As rewritten the code will only say done if the actual value in the field is 1.  Otherwise it says Not Done.  However, only the last record read will be compared for your if statement at this indentation level.  All other records will be read, but ignored for the test.  So I doubt this is what you really want to test.  Normally you would put the if test in the loop and break out of it when you find the value you want.  To do that the code would be:

fc = "Parcels" field = "GeneratedN" cursor = arcpy.SearchCursor(fc) for row in cursor:     Val = row.getValue(field)     if Val == 1:         print "Done"         break;     else:         print "Not Done" 

View solution in original post

0 Kudos
11 Replies
RichardFairhurst
MVP Honored Contributor
I want to use an if else statement to pass a condition and it's not returning any of the two values what im i doing wrong?

fc = "Parcels" field = "GeneratedN" cursor = arcpy.SearchCursor(fc) for row in cursor:     row.getValue(field) Val = row.getValue(field) if Val == 1:     print "Done" else:     print "Not Done" 


Any suggestions please


Your indentation is wrong for the Val = row.getValue(field) line.  It occurs only after every record is read and should produce an error.  It needs to replace the row.getValue(field) line at the same indentation level.

fc = "Parcels" field = "GeneratedN" cursor = arcpy.SearchCursor(fc) for row in cursor:     Val = row.getValue(field) if Val == 1:     print "Done" else:     print "Not Done" 


As rewritten the code will only say done if the actual value in the field is 1.  Otherwise it says Not Done.  However, only the last record read will be compared for your if statement at this indentation level.  All other records will be read, but ignored for the test.  So I doubt this is what you really want to test.  Normally you would put the if test in the loop and break out of it when you find the value you want.  To do that the code would be:

fc = "Parcels" field = "GeneratedN" cursor = arcpy.SearchCursor(fc) for row in cursor:     Val = row.getValue(field)     if Val == 1:         print "Done"         break;     else:         print "Not Done" 
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks rfairhur24 you suggestion(s) did the trick for me. Thanks a bunch
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks rfairhur24 you are the man!!!

Please can you help me with this problem, im trying to drop my pdf output into a nested folder created from my script and using the searchcursor to get attribute values this is my script;

fc = "Parcels"
field = "LGA"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    row.getValue(field)
val = row.getValue(field)
print (row.getValue(field))
field = "District"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    row.getValue(field)
val1 = row.getValue(field)
print (row.getValue(field))
field = "Block_No"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    row.getValue(field)
val2 = row.getValue(field)
print (row.getValue(field))
field = "Plot_No"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    row.getValue(field)
val3 = row.getValue(field)
print (row.getValue(field))
outName = str(val) + "_LGA" + "(" + str(val1) + " _Area" + ")"
outName1 = "Plot_" + str(val3) + "(" "Block_" + str(val2) + ")"
outName2 = "Block_" + str(val2) + "_Plot_" + str(val3) + "_" + str(val1) + "_Area_of_" + str(val) + "_LGA" + ".pdf"
arcpy.CreateFolder_management("C:\\ARA_STATE\\", outName)
arcpy.CreateFolder_management("C:\\ARA_STATE\\"+ outName,outName1)
arcpy.mapping.ExportToPDF(mxd,"C:\ARA_STATE\TDP_For_" + outName1)


my final pdf output is not going into something like this

arcpy.mapping.ExportToPDF(mxd,"C:\ARA_STATE\TDP_For_" + outName,outName1)


What do you think?
0 Kudos
RichardFairhurst
MVP Honored Contributor
arcpy.CreateFolder_management("C:\\ARA_STATE\\", outName)
arcpy.CreateFolder_management("C:\\ARA_STATE\\"+ outName,outName1)
arcpy.mapping.ExportToPDF(mxd,"C:\ARA_STATE\TDP_For_" + outName1)


my final pdf output is not going into something like this

arcpy.mapping.ExportToPDF(mxd,"C:\ARA_STATE\TDP_For_" + outName,outName1)


What do you think?


You switched from using double slashes to single in the PDF export line.  Double slashes are required for all paths in all methods unless you are place an "r" directly in front of the string to let Python know the string is to be read "raw" or unparsed.  So make your last line read either:

arcpy.mapping.ExportToPDF(mxd,"C:\\ARA_STATE\\TDP_For_" + outName,outName1)


or

arcpy.mapping.ExportToPDF(mxd,r"C:\ARA_STATE\TDP_For_" + outName,outName1)


Also, even if you use the "r" option, if the last character in the string is a "\" the Python help says it must always end with a double slash "\\".  So r"C:\ARA_STATE\" will cause an error, but r"C:\ARA_STATE\\" will work (note the single slash in the middle of the string is fine in both, but the double slash is required at the end of the string.)
0 Kudos
RichardFairhurst
MVP Honored Contributor
Looking at your code I see a lot of wasted lines related to your cursors that did not use the previous advice I gave.  If you mean to read values from all rows you need different indents and logic.  You do not need a cursor for each field.  You only need a new cursor for each feature class.  Since you are only dealing with one feature class you only need one cursor, but many (4) getValue calls when reading each row.  Here is my attempt to clean it up a bit

fc = "Parcels"
field = "LGA"
field1 = "District"
field2 = "Block_No"
field3 = "Plot_No"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    # do not use 4 cursors on the same feature class, only 1 cursor is needed.
    # read every cursor row and get all 4 field values in a single read of the row
    val = row.getValue(field)
    print (row.getValue(field))
    val1 = row.getValue(field1)
    print (row.getValue(field1))
    val2 = row.getValue(field2)
    print (row.getValue(field2))
    val3 = row.getValue(field3)
    print (row.getValue(field3))
    # The lines below must also be in the loop to use the values read from each row.
    outName = str(val) + "_LGA" + "(" + str(val1) + " _Area" + ")"
    outName1 = "Plot_" + str(val3) + "(" "Block_" + str(val2) + ")"
    outName2 = "Block_" + str(val2) + "_Plot_" + str(val3) + "_" + str(val1) + "_Area_of_" + str(val) + "_LGA" + ".pdf"
    arcpy.CreateFolder_management("C:\\ARA_STATE\\", outName)
    arcpy.CreateFolder_management("C:\\ARA_STATE\\"+ outName,outName1)
    arcpy.mapping.ExportToPDF(mxd,r"C:\ARA_STATE\TDP_For_" + outName2)


Since you are just learning Python, try to test smaller amounts of code to make sure you get results you expect.  It is much harder to issolate problems when you do a lot of lines of code before testing.

I assumed you meant to use outName2 for the pdf rather than outName1, since outName2 ends with the .pdf extension.  I think however, that you also meant to have the pdfs go into the new folders you are creating.  If so, for the last line you need something like:

    arcpy.mapping.ExportToPDF(mxd,r"C:\ARA_STATE\\" + outName + "\\" + outName1 + "\\" + "TDP_For_" + outName2)


You may also need to include some logic to do "Exists" testing on your directories, since you cannot create a directory that is already created without causing an error.
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
A very big thank you to you SIR!!!! Once again you are the main man. You solved yet another problem for me!
0 Kudos
RichardFairhurst
MVP Honored Contributor
A very big thank you to you SIR!!!! Once again you are the main man. You solved yet another problem for me!


Glad to help.  Consider adding a point to the response so that others will recognize that it was helpful to you.
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Yeah ive been trying to do so but i think i have issues with my browser--is it by clicking the vote up button?
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Dear rfairhur24 please i want to be able to select a name from a drop down list of a combobox i created and i use to use the enter tab to set the process and not by just selecting the name.  which of the following codes would i use? Thanks

def onEditChange(self, text):
                pass
            def onFocus(self, focused):
                pass
            def onEnter(self):
                pass
            def refresh(self):
                pass
0 Kudos