Please help with update cursor. So close I can taste it.

4454
2
05-16-2016 04:39 AM
HarryKlein
New Contributor

Hello,

I need some help with this script I am developing. I need the update cursor to overwrite the empty LOCALITY values in TWB_Property with the ADMINAREA from TWB_Suburbs that I have turned into a list. I know the code is a little messy and I also can not figure out how to display it all using this forum. Any help will be appreciated. Thank you.

code begins below.

# Import modules
import arcpy
import os

#Set workspace geodatabase
arcpy.env.workspace = "C:\Users\Harry\Documents\Uni\Semester 1 2016\GIS3407 GIS Programming and Visualisation\Assignment 2\Assignment2.gdb"

#Start an editing session
edit = arcpy.da.Editor("C:\Users\Harry\Documents\Uni\Semester 1 2016\GIS3407 GIS Programming and Visualisation\Assignment 2\Assignment2.gdb")
edit.startEditing(False, False)

# Create a list of suburbs
# Create an empty list
localityList = []

#Create the search cursor
rows = arcpy.SearchCursor("TWB_Suburbs")

#Move to the first row
row = rows.next()

#Append the list with the selected locality name
while row:
    localityList.append(str(row.ADMINAREA))
    #Move to the next row
    row = rows.next()

print(str(len(localityList)) + " <-- # of suburbs")

del row, rows
# Make a layer of all of the properties with no locality value
# Create query
nullQuery = '"LOCALITY" = \' \''

# Create layer
nullLayer = arcpy.MakeFeatureLayer_management("TWB_Property", "TWB_Property_Layer", nullQuery)
field = "LOCALITY"
fc = "TWB_Property_Layer"

#Counter
counter = 0

# Select by location
indexCounter = 0
print localityList
for counter in range(0,len(localityList)):
    localityQuery = '"ADMINAREA" = ' + '\''+ localityList[indexCounter] + '\''
    localityLayer = arcpy.MakeFeatureLayer_management("TWB_Suburbs", "TWB_Sububs_Layer", localityQuery)
    arcpy.SelectLayerByLocation_management("TWB_Property_Layer", "WITHIN", "TWB_Sububs_Layer")

    parcelList = []
    sRows = arcpy.SearchCursor("TWB_Property_Layer")
    sRow = sRows.next()

    while sRow:
            parcelList.append(sRow)
            sRow = sRows.next()
    print ("There are " + str(len(parcelList)) + " empty locality fields in " + localityList[indexCounter])
    numberParcel = int(len(parcelList))
    with arcpy.da.UpdateCursor(fc, field) as cursor:
            for uRow in cursor:
                if uRow[0] == '':
                    uRow[0] = str(localityList[indexCounter])
                    cursor.updateRow(uRow)

    arcpy.Delete_management("TWB_Sububs_Layer")
    counter += 1
    indexCounter += 1

del uRow

arcpy.Delete_management("TWB_Property_Layer")
arcpy.Delete_management("TWB_Sububs_Layer")
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

Did you get an error message?

I am surprised it gets past your paths... they need to be in raw format ie   r"c:\yourpathhere\your.gdb"

and you would be advised to use paths that don't contain spaces or other stuff.

for paths Filenames and file paths in Python

code formatting Code Formatting... the basics++

curtvprice
MVP Esteemed Contributor

First just a piece of advice - Python string substitution is much easier than creating queries by adding strings:

localityQuery = '"ADMINAREA" = ' + '\''+ localityList[indexCounter] + '\'' 
localityQuery = '"ADMINAREA" = \'{}\''.format(localityList[indexCounter])  # better

Also you may do better in the update query by checking for null values (not just blank):

    with arcpy.da.UpdateCursor(fc, field) as cursor: 
            for uRow in cursor: 
                if uRow[0] in ['', None]: 
                    uRow[0] = str(localityList[indexCounter]) 
                    cursor.updateRow(uRow)