4

I am trying to run a field calculator python script that will take a value from one field in the attributes table and create an abbreviation in another field that was designated for it.

Here is a example of the code that was written for this function:

enter image description here

def checkValue(c,d):
    if c == 'ARTERIAL_CRACKSEAL':
        return 'ACS'

The problem is that once I created coded value domains for these attributes the code stopped working (i.e. code:1 description: ARTERIAL_CRACKSEAL ; code:2 description ARTERIAL_microseal ; etc.). Would anyone know how to adjust the code so the script runs successfully? I need to keep coded value domains for this attribute field.

  • 1
    Please edit your question to include your code as text rather than just as an image. Code as text makes it easier for potential answerers to copy/paste your code snippet to test, instead of having to type it all out manually. – Midavalo Sep 8 '16 at 18:06
6

In a coded value domain, the values are stored as 1, 2, 3 and not as ARTERIAL_CRACKSEAL, ARTERIAL_MICROSEAL, ARTERIAL_OVERLAY.

To make your code work with the coded values, change your if lines to refer to the codes:

def checkValue(c, d):
    if c == 1:
        return 'ACS'
    if c == 2:
        return 'AMS'
    if c == 3:
        return 'AOL'
    if c == 4:
        return 'TRM'
    if c == 5:
        return 'FST'
  • I have tried that as well and it did not seem to work unfortunately. – Davor Romic Sep 8 '16 at 18:17
  • What errors did it return? can you include a screenshot of your domain settings and some of your data? – Midavalo Sep 8 '16 at 18:19
  • 1
    You may have already checked this, but make sure the types of your coded values and values in your script match. In this example, make sure your coded values are stored as integers and not characters. – mmoore Sep 8 '16 at 18:22
  • I have tried it again by using the coded values instead of the description and including quotation marks around them. It worked fine this time, so your first answer was correct. I'm not sure if it was just the quotation marks or I did something else when I tried yesterday. Thank you for your help. – Davor Romic Sep 8 '16 at 20:50
  • 1
    @DavorRomic if this has worked for you, please consider marking it as the answer. See What should I do when someone answers my question? – Midavalo Sep 9 '16 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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