AnsweredAssumed Answered

wanted: easyest way for field calculation with python

Question asked by DMMotti on Dec 5, 2018
Latest reply on Jan 18, 2019 by LukeW



I'd like to do some complicated field calculation.

I want to calculate the Field 6-value (see table below, representing a FeatureClass table) as follows:


If Field 1 = 617: Field 3-value

Else: Field 2-value






If Field 4 = ABC:

      If Field 5 is Null:



            "Someothertext" + Field 5



I entered the results in Field 6 as an example

Field 1Field 2Field 3Field 4Field 5Field 6
611from July 1April to NovemberTextWith_äöüfrom July 1 |  Sometext
612from July 1April to NovemberABCTextWith_äöüfrom July 1 | Someothertext TextWith_äöü
612from July 15April to NovemberABCfrom July 15 | Someothertext
617from July 1April to NovemberABCApril to November | Someothertext



Up until now, I exported the table and calculated the value with some "when"-statements in Excel. But since I'd like to have the results in GIS i searched for an other solution.


I ended up using "arcpy.CalculateField_management" and something like this:

def compare(class1, class2, class3):

   if class1 in (612, 611):
       return class2
   elif class1 == 617:
       return class3
       return class1

# Expression below

compare(!some_field!, !another_field!, !another_field2!)

This didn't work quite well because

- the code will become huge (I have more fields and values to compare)

- there seem to be problems with converting/using characters which don't fit into ASCII

- i couldn't figure out how to ask properly if Field 5 is Null

- I could not properly translate the "AND" and the "OR" from Excel to Python (for example: If Field 4 is Null AND Field 5 is Null AND Field 1 is 612, Field 6 = ...)




Is there an other, easyer solution how I can calculate Field 6 value?


Thanks a lot for your help!