Hey Joshua
Thank you a lot for all of your help!
After several hours of code typing, (at least in my opinion / compared to the excel-code) a kind of large code and several things not working (properly), I asked myself if there might be an easyer way to write the code. That's why I started thinking if I'm actually really using the easyest way to do it and therefore I opened this new thread.
I'm not sure if that makes a difference: I'm using ArcGIS 10.3 with python 2.7.8.
I imported my example-table as a table to a GDB and run your code. It did not work:
"Messages
Executing: CalculateField Tabelle Field_6 compare(!Field_1!, !Field_2!, !Field_3!, !Field_4!, !Field_5!) PYTHON_9.3 "def combine (fld1, fld2, fld3, fld4, fld5):\n part1 = (\n fld3 \n if fld1 == 617\n else fld2\n )\n part2 = (\n (\n u"Sometext"\n if fld5 is None\n else u"Someothertext" + fld5\n )\n if fld4 == u"ABC"\n else u"Sometext"\n )\n\n return u"{} | {}".format(part1, part2)"
Start Time: Thu Dec 06 11:04:19 2018
ERROR 000539: Error running expression: compare(612.0, u"from July 1", u"April to November", u"ABC", u"TextWith_äöü")
Traceback (most recent call last):
File "<expression>", line 1, in <module>
TypeError: compare() takes exactly 3 arguments (5 given)
Failed to execute (CalculateField).
Failed at Thu Dec 06 11:04:19 2018 (Elapsed Time: 0.02 seconds)"
I can't figure out why it thinks that not all the arguments would be used...?
Anyway, I tried to simplify my code based on your model.
This works perfectly:
def compare(Code, SchnittZP, WeideZP, NHG_YN):
part1 = (
"-"
if Code in (921, 922, 923, 924)
else "ERROR"
)
return part1
then I tried to enter an other argument (line 6+7) - resulting in an error "ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 7) Failed to execute (CalculateField). ":
def compare(Code, SchnittZP, WeideZP, NHG_YN):
part1 = (
"-"
if Code in (921, 922, 923, 924)
"Text 634"
elif Code == 634
else "Wrong code"
)
return part1p
So I thought, maybe I need to adjust the whole code like this:
def compare(Code, SchnittZP, WeideZP, NHG_YN):
part1 = (
if Code in (921, 922, 923, 924):
"-"
elif Code == 634:
"Text 634"
else:
"Wrong code"
)
return part1
because I've never seen the order you put the result bevore the condition. (What's the difference? Why do you prefer result bevore condition? How can you enter en elif-statement?)
Anyway it gives back following error: "Runtime error Traceback (most recent call last): File "<string>", line 56, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 3453, in CalculateField raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 4) Failed to execute (CalculateField). "
I tried to handle this by changing row 4 to
value = "-"
and in the end "return value" but it didn't change anything.
Thanks for the other useful hints about "my concerns"!
I will try to implement some of your hints and explain my problems with unicode later 🙂