import arcpy # Change these 2 input parameters accordingly... fc = r'E:\schoolTestData\test.gdb\schoolJoinToPolys' outField = 'outGrades' delField = arcpy.AddFieldDelimiters(fc, outField) # This query works whether shapefile or gdb fc. query = delField + ' IS NOT NULL AND ' + delField + ' NOT IN (\' \')' rows = arcpy.UpdateCursor(fc, query) for row in rows: # This populates a list, row by row... # outGradesRaw will potentially contain 'P' vals. # outGrades is a new list with 'P' vals eliminated. outGradesRaw = row.getValue(outField).split(',') outGrades = [] for each in outGradesRaw: if not each[0:1] == 'P': outGrades.append(each) # sorted outGrades for loop, comparison of successive items outGrades.sort() # initializing count count = 0 compareVal = outGrades[0] # Loop over the sorted list to count. for each in outGrades: if each == compareVal: count += 1 else: # This conditionally sets the fieldName to load... if not compareVal[0:1] == 'K': fieldName = 'Grade_' + str(int(compareVal)) else: fieldName = 'Grade_K' # Successive K vals may not match, # so will be temp stored in att table, # then fetched to make a cumulative count. count += row.getValue(fieldName) # count committed to row obj from att table row.setValue(fieldName, count) # row obj committed to cursor obj of att table rows.updateRow(row) # resetting count to 1 count = 1 # setting compareVal to current val compareVal = each # load the count for the last val, identical to part of above block if not compareVal[0:1] == 'K': fieldName = 'Grade_' + str(int(compareVal)) else: fieldName = 'Grade_K' count += row.getValue(fieldName) row.setValue(fieldName, count) rows.updateRow(row) del rows
>>> import arcpy >>> shp = r'E:\schoolTestData\schoolJoinToPolys4.shp' >>> query = arcpy.AddFieldDelimiters(shp, 'outGrades') + ' NOT IN (\' \')' >>> print query "outGrades" NOT IN (' ') >>> rows = arcpy.UpdateCursor(shp, query) >>> for row in rows: outGrades = row.outGrades.split(',') outGrades.sort() count = 0 compareVal = outGrades[0] for each in outGrades: if each == compareVal: count += 1 else: if not compareVal in ['KA', 'KP', 'PA', 'PP']: fieldName = 'Grade_' + str(int(compareVal)) else: fieldName = 'Grade_K' count += row.getValue(fieldName) row.setValue(fieldName, count) rows.updateRow(row) count = 1 compareVal = each # load the last unique val if not compareVal in ['KA', 'KP', 'PA', 'PP']: fieldName = 'Grade_' + str(int(compareVal)) else: fieldName = 'Grade_K' count += row.getValue(fieldName) row.setValue(fieldName, count) rows.updateRow(row) >>> del row, rows >>>
Traceback (most recent call last): File "<pyshell#81>", line 21, in <module> fieldName = 'Grade_' + str(int(compareVal)) ValueError: invalid literal for int() with base 10: ''
if not compareVal in ['KA', 'KP', 'PA', 'PP']: fieldName = 'Grade_' + str(int(compareVal)) else: fieldName = 'Grade_K' count += row.getValue(fieldName)
if not compareVal in ['KA', 'KP', 'PA', 'PP']: fieldName = 'Grade_' + str(int(compareVal)) elif not compareVal in ['PA', 'PP']: fieldName = 'Grade_K' count += row.getValue(fieldName)
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.