2

I am very new to Python and need what I believe is a very basic script in order for my field to represent another fields' input, if the field's value is 0. I tried to search for this, but gave up after two hours... I am currently trying the script on two fields that I made only for this purpose, using ArcGIS 10.0. So what I want to do is returning the value of field "TESTFIELDBEREGNER" in "TESTFIELD" IF TESTFIELD=0, else return TESTFIELD. I believe the error may lie in the definition, as I don't know how to define strings in this correctly? The geoprocessing results window tells me there is a syntax error in line 2.

def RemoveNULLS(x, v):
  if x == '0'
    return v 
  else:
    return x

 RemoveNULLS( !TESTFIELD!, !TESTFIELDBEREGNER!)
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Max Guddat
  • 23
  • 2

2 Answers2

3

The code should work, just add the colon after if statement and proper indentation:

def RemoveNULLS(x, v):
    if x == '0':
        return v
    else:
        return x

RemoveNULLS( !TESTFIELD!, !TESTFIELDBEREGNER!)

Be careful with what data type used for the x (if it is an integer, you cannot use '0', should be 0).

Alex Tereshenkov
  • 29,912
  • 4
  • 54
  • 119
  • Thanks so much!!! It seems I was simply missing the ":" after the if statement.. Made my day!! – Max Guddat Oct 06 '14 at 09:51
  • No problem. Check this post on how get started learning arcpy and Python for GIS: http://gis.stackexchange.com/questions/53816/what-are-some-resources-for-learning-arcpy, it is a great place to start! – Alex Tereshenkov Oct 06 '14 at 10:26
0

Note that in your case you could also select by attribute "TESTFIELD"='0' (select by attribute), then update only those values with field calculator. If you have a vera large table and very few cases with "TESTFIELD"='0' , it could be faster.

radouxju
  • 49,636
  • 2
  • 71
  • 144