Thursday, 28 April 2016

arcgis desktop - Recalculate a Field with data from another field, ignoring NULL values


I have a Feature Class in a Geodatabase with an field for pipe diameter. I also have a shapefile with a field for diameter. The shapefile is from field verification work that verified a variety of pipe characteristics. I have joined the shapefile to the Feature Class in ArcMap, see the image below. I would like to recalculate using Field Calculator and the Python Parser the first diameter field with that information in the second diameter field, any null values would keep the original data in the first diameter field. Through some research I have hodgepodged the following script that fails upon execution when using the Filed Calculator. ArcMap spits out an Error:



Python syntax error: Parsing error SyntaxError: invalid syntax (line 1)




enter image description here


def IgnoreNull (GravitySewer.Diameter, assets.Diameter)
if assets.Diameter is None:
return GravitySewer.Diameter
else:
return assets.Diameter

enter image description here



Answer



You need to change bottom line in field calculator to:



IgnoreNull(!GravitySewer.Diameter!, !assets.Diameter!)

You could also have a problem with the dots in the function variable names. Try changing them in the codeblock to:


def IgnoreNull (GravitySewer_Diameter, assets_Diameter):
if assets_Diameter is None:
return GravitySewer_Diameter
else:
return assets_Diameter

You can name them whatever you want. It is when you call the function you need the real field names enclosed in !!



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...