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.
- import arcpy, os
- from arcpy import env
- env.workspace = r"C:\temp\python\test.gdb"
- arcpy.env.qualifiedFieldNames = False
- arcpy.env.overwriteOutput = True
- BldSelection = "BuildingFootprints"
- Bld = arcpy.env.workspace + os.sep + "BuildingFootprints" #target point feature class
- BldCount = int(arcpy.GetCount_management(Bld).getOutput(0))
- dsc = arcpy.Describe(BldSelection)
- selection_set = dsc.FIDSet
- if len(selection_set) == 0:
- print "There are no features selected"
- elif BldCount >= 1:
- #Gets the highest AddressID and calculates new point to assign new highest AddressID
- 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' + str(Bld_ID)
- Bld_ID += 1
- rows.updateRow(row)
- del row
- del rows
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] == ' ':
You need to learn something about nothing...
nothing = None, "", {}, []
obj = None # or "" '' [] et if not obj: print("something here") else: print("nothing object")
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()
Ok... clearly
obj is an object... any object ... pick one
if obj:
if not obj:
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.
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.
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.
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.