0

I'm writing a standalone Python script to use for labeling ArcMap 10.7 data. (Want to avoid label expressions for portability, etc). I'm attempting to use ID attribute to label based on ADMIN_ORG attribute data.

I have found from SO, that nested 'with' statements are better for this than nested 'for' statements. I tried to replicated the answers from this post ArcPy nested loop problem still having issues....

The script currently DOES print the data from the ID column as expected but errors at the end at

"for label in sc_label:" Runtime Error: A column was specified that does not exist. But the column absolutely does and was recognized in earlier attempts. What am I missing?

def newlabel():
    fields = ['ID', 'ADMIN_ORG']
    label = fields[0]
    region = fields[1]
    with arcpy.da.SearchCursor(fc, region) as sc_region:
        for region in sc_region:
            with arcpy.da.SearchCursor(fc, label) as sc_label:
                for label in sc_label:#errors here but still prints the attribute data
                    print str(label[0])
2
  • 1
    Also you use label as a field name and as a row from the sc_label.
    – Hornbydd
    Jun 17, 2020 at 22:10
  • 1
    I think the re-use of the parameter name (as commented by @Hornbydd above) is the reason for the error. Using a different parameter name in the cursor may avoid the error.
    – Midavalo
    Jun 19, 2020 at 3:45

1 Answer 1

2

I think if you add some more info to your question it would help to find an answer. What sort of output or state are you expecting to have after you run this code? Is there a reason you need two search cursors? As the code stands, the first one is not accomplishing anything. You may be able to create your label like this:

def newlabel():
    fields = ['ID', 'ADMIN_ORG']
    with arcpy.da.SearchCursor(fc, fields) as sc:
        for row in sc:
            # Do something with row[1] to create label:
            label = '{}: {}'.format(row[1], row[0])
            print str(label)

Keep in mind however that this is just printing the labels, not sending them anywhere to be used. If you wanted to store them in a list you might do something like this:

def newlabels():
    fields = ['ID', 'ADMIN_ORG']
    labels = []
    with arcpy.da.SearchCursor(fc, fields) as sc:
        for row in sc:
            # Do something with row[1] to create label:
            label = '{}: {}'.format(row[1], row[0])
            labels.append(label)
    return labels
5
  • 1
    Spot on, but the user should still test for f in fields: if len(arcpy.ListFields(f)) == 0: return '' # or something else just as meaningless, you still have to return something. This conclusively determines if the fields with the given names do in fact exist within the feature class and there isn't a typo or using a fields aliasName instead resources.arcgis.com/en/help/main/10.2/index.html#//…. Jun 17, 2020 at 23:27
  • Thanks for the response. I did just post my most recent snippet attempt. To clarify I am trying to search the ADMIN field, if it starts with '04' I want to label the map with parsed ID field eliminating preceeding 0s , etc. if it starts with '06' label with other etc. I was just trying to print it to test the syntax. Do I need to store the ID labels in a field? Also - I was trying to differentiate 'rows' in ID field from 'rows' in ADMIN field (using label, & region) do I NEED to use the syntax 'row'? How does python know which 'row' to use... is that from the search cursor assignment? Jun 18, 2020 at 14:35
  • This is at the end of my original script showing labeling attempt. #return str.join('', label).replace("-A-ADMIN","")#.replace("-ADMIN","")#.label[2:].lstrip('0').replace('.', '-').lstrip(ascii_letters) Jun 18, 2020 at 14:44
  • I think we've basically answered your original question @CödingersCat as you asked it but I do realize this probably did not solve your problem. It sounds like you might be better served by posting a new question with a brief summary of what your data fields are and how you are hoping to label each feature. In other words, where you are starting from and where you'd like to end up. You can certainly add what you've tried so far (perhaps with a link to this question) but I think you'll get to a solution faster by posing your question that way instead.
    – jslatane
    Jun 19, 2020 at 1:53
  • @CödingersCat the row in the code is just a parameter name. Keeping everything in one cursor will be more efficient, and there doesn't appear to be any reason for the first cursor in your code. If you want to refer to label or region fields within the cursor you can just specify that with row[0] for label and row[1] for region (these are their places in your fields list).
    – Midavalo
    Jun 19, 2020 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.