Tuesday, 5 January 2016

arcgis 10.1 - Concatenate only when all fields to be concatenated have values


I have two fields that I want to concatenate. Field1 contains null values and Field2 has values for every record. I want to concatenate only when both fields have values. I tried using a function with an if/else statement but that doesn't work as the resulting concatenation will return values from field2.


def concat_fields(field1, field2):
if field1 == ""
return

else:
return field1 + field2

I know this is wrong. Any suggestions?



EDIT:


enter image description here



Answer



Based on your results, it looks like field1 may have a space (" ") in the field, instead of an empty string.


def concat_fields(field1, field2):
if field1.strip() == "" or field2.strip() == "":
return ""
else:
return field1 + field2

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...