Select to view content in your preferred language

If-then-else

4209
16
01-07-2016 11:11 AM
CCWeedcontrol
Frequent Contributor

I need help setting up an if-than-else within a if-else statement. I need the code i need to check if selected features has any text inside the AddressID field if it does have any text inside the field i need it to by pass the with arcpy.da.SearchCursor and the with arcpy.da.UpdateCursor and move on to the next process. if doesn't have any text inside the field i need it to preform the with arcpy.da.SearchCursor and the with arcpy.da.UpdateCursor. I would appreciate any help.

  1. import arcpy, os 
  2. from arcpy import env 
  3. env.workspace = r"C:\temp\python\test.gdb" 
  4.  
  5. arcpy.env.qualifiedFieldNames = False         
  6. arcpy.env.overwriteOutput = True   
  7.  
  8. BldSelection = "BuildingFootprints"          
  9. Bld = arcpy.env.workspace + os.sep + "BuildingFootprints" #target point feature class   
  10.    
  11. BldCount = int(arcpy.GetCount_management(Bld).getOutput(0))   
  12.    
  13. dsc = arcpy.Describe(BldSelection)    
  14. selection_set = dsc.FIDSet            
  15. if len(selection_set) == 0:          
  16.     print "There are no features selected"     
  17.                 
  18. elif BldCount >= 1:   
  19.     #Gets the highest AddressID and calculates new point to assign new highest AddressID   
  20.     Bld_list = []         
  21.     with arcpy.da.SearchCursor(Bld, ["Bld_ID"]) as cursor:         
  22.         for row in cursor: 
  23.             try:         
  24.                 if "Bld" in row[0]: 
  25.                      
  26.                     Bld_list.append(int(row[0].strip("Bld")))            
  27.             except TypeError:         
  28.                 pass                 
  29.     del cursor         
  30.  
  31.     print Bld_list 
  32.          
  33.     Bld_list.sort()         
  34.     Bld_ID = Bld_list[-1] + 1         
  35.        
  36.    
  37.     with arcpy.da.UpdateCursor(BldSelection, "Bld_ID") as rows:          
  38.         for row in rows:             
  39.             row[0] = 'Bld' + str(Bld_ID) 
  40.             Bld_ID += 1                     
  41.             rows.updateRow(row)     
  42.         del row          
  43.         del rows  
0 Kudos
16 Replies
CCWeedcontrol
Frequent Contributor

I have tried the following for blank and null attributes for line 36, but i am getting error.  Parsing error SyntaxError: invalid syntax

with arcpy.da.SearchCursor(BldSelection , "Bld_ID") as cursor:  
        for row in cursor:  
            if row[0] > '' or row[0] Null <":

and

with arcpy.da.SearchCursor(BldSelection , "Bld_ID") as cursor:  
        for row in cursor:  
            if row[0] > '' :
               if row[0] Null <":

I have tried, but nothing happends, the Bld_ID of the selected feature doesn't update.

 with arcpy.da.SearchCursor(BldSelection , "Bld_ID") as cursor:  
        for row in cursor:  
            if row[0] > None or row[0] == ' ':
0 Kudos
DanPatterson_Retired
MVP Emeritus

You need to learn something about nothing...

nothing = None, "", {}, []

obj = None  # or "" '' [] et
if not obj:
    print("something here")
else:
    print("nothing object")
CCWeedcontrol
Frequent Contributor

I have tried the following but nothing happens, no error

obj = None, "", ' '
    with arcpy.da.SearchCursor(BldSelection , "Bld_ID") as cursor:  
        for row in cursor:  
            if not obj:
                    Bld_list = []          
                    with arcpy.da.SearchCursor(Bld, ["Bld_ID"]) as cursor:          
                        for row in cursor:  
                            try:          
                                if "Bld" in row[0]:  
                                      
                                    Bld_list.append(int(row[0].strip("Bld")))             
                            except TypeError:          
                                pass                  
                    del cursor     
                     print Bld_list  
                          
                    Bld_list.sort()          
                    Bld_ID = Bld_list[-1] + 1          
                        
                    
                    with arcpy.da.UpdateCursor(BldSelection, "Bld_ID") as rows:           
                        for row in rows:              
                            row[0] = 'Bld%05d' %Bld_ID 
                            Bld_ID += 1                      
                            rows.updateRow(row)      
                        del row           
                        del rows

            else: #other process sptial join
                # Stop the edit operation.
                edit.stopOperation()

                  
0 Kudos
DanPatterson_Retired
MVP Emeritus

Ok... clearly

obj  is an object... any object ... pick one

  • it will either have a null state, such as empty strings ( '', ""), lists dictionaries  or sets ( [], {} () ) or...
  • it will be null, like None, np.Nan etc etc

if obj

  • means if the object is something or has a non-null values or state.... do something in the lines that immediately follow
  • conclusion?  if the value is something then do something with it

if not obj:

  • if the object is null, then do something.  this condition is used to do "stuff" when objects are null

Confused?????  Never check the code you are trying to get working to see if the logic works... you will just keep spinning your cerebral wheels.  always check the logic you are working on, using something that you know works.

CCWeedcontrol
Frequent Contributor

Dan I uderstad what you and Wes are saying with the explanations/logic I am just not sure how to put it into the current code to reflect what I am trying to accomplish.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I have lost sight of...or not fully grasped... the need for the code in the first place.  This appears to be a solution to a one-off or seldom performed task.  I advise breaking it into parts that you can solve even if it means that you will have to perform several individual steps rather than one automagic solution.

0 Kudos
WesMiller
Deactivated User

In support of what Dan is saying it appears to be a simple select by attribute followed by a field calculate. The whole process shouldn't take more than a minute.

0 Kudos