0

I am working in SDE geodatabase where I have a feature class 'Mange_Waste' and "Q1", "Q2" are two fields. I wanted to use python script for populating "Q2" from "Q1" with a condition. Like, if the value in "Q1" is "a", then it should return "1" in "Q2". I have hundreds of records in "Q1".

I am using following code however, it's giving me syntax error.

If Q1 == "a":
   Q2 = 1
else :
   Q2 = 0

Kindly tell me what I am doing wrong and what should be the correct code.

4

you need to use a block or to write everything in a single line (respecting the indentations)

block (with Python parser)

def myfunction(a,b):
    if a == 'a':
        return 1 
    else:
        return 0 

then you call the function defined in your block

myfunction(!Q1!, !Q2!)

note that this is a general solution, but in your specific case you could simply use

!Q1!=='a'

in the field calculator (with Python parser). The boolean true/false will be 1/0 in an integer field.

3

Since you tagged this with python, you can also accomplish this with an updatecursor. Using field calculator in python for multiline statements is sometimes more difficult than the same function in a cursor.

with arcpy.da.UpdateCursor("Mange_Waste", ("Q1", "Q2")) as cursor:
   for row in cursor:
      if row[0] == "a":
         row[1] = 1
      else:
         row[1] = 0
      cursor.updateRow(row)

Alternatively:

with arcpy.da.UpdateCursor("Mange_Waste", ("Q1", "Q2")) as cursor:
   for row in cursor:
      row[1] = 1 if row[0] == "a" else 0
      cursor.updateRow(row)
1

Your logic is correct. This help guide may help you out in the future for using python in the field calculator. There are already valid pythonic answers above I'll post another option, in VB (sometime's it is simplier than python).

In field calculator choose "show codeblock" for "Q2" under "Pre-logic script code" write:

In VB:

if [Q1] = "a" then
  result = "1"
else
  result = "0"
end if

Under Q2 = write:

result
7
  • -1 I don't believe this has ever worked in a version of Arc* as you can't directly reference field names in a codeblock. Python doesn't even support end if! – Paul Jul 14 '14 at 17:21
  • You can definitely reference field names in a codeblock. Your -1 is unwarranted. – GISKid Jul 14 '14 at 17:24
  • However I did double check my code and you're right about the end if. I'll edit – GISKid Jul 14 '14 at 17:29
  • See here. Specifically the comments by Jason and rad. – Paul Jul 14 '14 at 17:29
  • Now that you've swapped to VB it works, but my -1 remains as OP indicated python. You cannot use that code with the python interpreter as it's an invalid function. The codeblock only works if the function is syntactically correct and no python interpreter will like a variable enclosed with bangs. – Paul Jul 14 '14 at 17:41

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.