0

I have problem with Field Calculator, using ArcGIS 10.2. I tried to write a statement but I don't know what is wrong.

I have two columns:

The first contains a number code. Every code represents some text data. For example: "1" means "trees,etc."

The second column (string) is empty and should contain text data. Could you help me with Python ?

enter image description here

2
  • 3
    Publish the Python code you are running, we will take a look. Aug 25, 2015 at 14:56
  • Below i pasted code Aug 25, 2015 at 15:43

1 Answer 1

4

Your code is not correctly formatted Python code:

  1. Indentation is important in Python, as it defines where functions and conditions start and end.
  2. You need a : after the function definition
  3. The correct operator for equality-comparison is the == and not simply = which assigns a value to a variable.

Also, with respect to the field calculator in ArcGIS, if you use a function to calculate your value, you also will need to check the "Show Codeblock" check box.

In your case, you have two possibilities:

  1. Your if/else-statement is very simple, so you could rewrite in a single Python line, and there is no need for a codeblock:

    "SampleText" if !Coded! == 1 else "SampleText_2"
    
  2. Using a code block, with the code block checkbox activated, the code block should be:

    def Text_d(Coded):
        if Coded == 1:
            return "SampleText"
        else:
            return "SampleText_2"
    

    and then in the "Coded=" box below you would need to write:

    Text_d(!Coded!)
    

    This second method gives you the possibility to use many Python functionalities such as nested conditions:

    def Text_d(Coded):
        if Coded == 1:
            return "SampleText"
        elif Coded == 2:
            return "SampleText_2"
        else:
            return "SampleText_3"
    
3
  • Ok, that way helped me but how to calculate for more conditions? I mean more "if`s", f.e 1 =="sample text_1", 2 == "sample_text_2", etc. Aug 25, 2015 at 16:54
  • I tried use it for more conditions and didnt work :/ Any idea whats wrong? Aug 25, 2015 at 17:09
  • It was mistake in last line of code (else Coded == 3, should be else:) Aug 25, 2015 at 17:23

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.