Hello,
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
AND
"|"
AND
If Field 4 = ABC:
If Field 5 is Null:
"Sometext"
Else:
"Someothertext" + Field 5
Else:
I entered the results in Field 6 as an example
| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 |
|---|
| 611 | from July 1 | April to November | | TextWith_äöü | from July 1 | Sometext |
| 612 | from July 1 | April to November | ABC | TextWith_äöü | from July 1 | Someothertext TextWith_äöü |
| 612 | from July 15 | April to November | ABC | | from July 15 | Someothertext |
| 617 | from July 1 | April to November | ABC | | April 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:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">compare</SPAN><SPAN class="punctuation token">(</SPAN>class1<SPAN class="punctuation token">,</SPAN> class2<SPAN class="punctuation token">,</SPAN> class3<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> class1 <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">612</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">611</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">return</SPAN> class2
<SPAN class="keyword token">elif</SPAN> class1 <SPAN class="operator token">==</SPAN> <SPAN class="number token">617</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">return</SPAN> class3
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">return</SPAN> class1
<SPAN class="comment token"># Expression below</SPAN>
compare<SPAN class="punctuation token">(</SPAN>!some_field!<SPAN class="punctuation token">,</SPAN> !another_field!<SPAN class="punctuation token">,</SPAN> !another_field2!<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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!