I'm attempting to create labels for a feature class that labels each feature "[ParentFeatureName] - [ThisFeatureID]". I found a code snippet on Esri support that should fit the bill (with the variable names and return changed as needed):
def FindLabel ([keyField], [FirstLabel]): import arcpy key1 = [keyField] # Key field in feature class key2 = "ID" # Key field in related table L = [FirstLabel] # Label field in feature class L2 = "Label2" # Label field in related table myDataTable = r"ExampleTable" # Path to related table cur = arcpy.da.SearchCursor(myDataTable, [key2, L2]) for row in cur: if str(key1) == str(row): L = L + " " + str(row) return L
But no labels appeared! So I did some experimenting:
- I did a sanity-check in the Python pane to make sure that the comparison is valid (it is)
- I started commenting out code to see where it bombed.
I had expected that commenting out the
for loop and the assignment of L would label my features with L, but to my surprise, there were still no labels. Only when I also commented out
cur = arcpy.da.SearchCursor(myDataTable, [key2, L2]) did the labels appear!
The same behavior also happens using the legacy arcpy.SearchCursor.
Why is that line of code preventing the labels from appearing and how do I work around it?