# Calculate Field using Fieldnames(variables?!)

the Question I have is kind of asked before. But I couldn't figure out the solution.

ArcGIS 10.2.2 Python 2.7.5

I`m adding fields to a table using user input for the fieldnames. Then I populate those fields and calculate with these fields yet another field.

It looks like this:

``````fieldname1 = arcpy.GetParameterAsText(2)
fieldname2 = arcpy.GetParameterAsText(3)

"", "NULLABLE")
"", "NULLABLE")
``````

In the tool the datatype for the user input is set to "String".

Then I use search and update cursor to populate those fields. Afterwards I want to calculate the next field, so I build a codeblock and an expression and use CalculateField_management:

``````codeblock = """def myFunction(mthree, mtwo):
if mthree >= 1 or mtwo >= 3:
return -3
elif mthree == 0 and (mtwo==1 or mtwo==2):
return -2
elif mthree == 0 and mtwo < 3 and mone >= 5:
return -2
if mthree == 0 and mtwo == 0 and mone ==4:
return -1"""
expression = 'myFunction(fieldname1, fieldname2)'
arcpy.CalculateField_management(FC, "EPK_Gesamt",expression,"PYTHON_9.3",codeblock)
``````

and that doesn't work. I tried several thing such as:

``````fieldn1 = '"!{}!"'.format(fieldname1)
fieldn2 = '"!{}!"'.format(fieldname2)
expression = 'myFunction(fieldn1, fieldn2)'
``````

or:

``````expression = "myFunction('!' + fieldname1 + '!','!' + fieldname2 + '!')'
``````

In both cases python returns me text values instead of fieldnames which doesn't work for the calculation in the calculatefield tool.

It works fine if I use the actual fieldnames in the expression variable:

``````expression = 'myFunction(!example1!, !example2!)'
``````

But of course I want to use the user input, so the user doesnt have to work in the script. Anyone knows how to deal with that?

• Why not use the update cursor instead of CalculateField? You'd get a cleaner and shorter code. Aug 25, 2015 at 14:39
• I thought about it, but there is the problem, that the function changes for every project I'm working with. A calculation will always be done, but the mathematical operators will change everytime. So I tought, it would be the easiest way for my colleagues to just create the function in the python shell, check every step individually, and then copy paste the whole thing into the script. Aug 26, 2015 at 9:56
• I see, this does make sense. Aug 26, 2015 at 10:32

This should work for you:

``````import arcpy

fc = r"C:\Users\us\Documents\ArcGIS\Default.gdb\_PointDistanceFc"
field1 = arcpy.GetParameterAsText(0)
field2 = arcpy.GetParameterAsText(1)

codeblock = """def myFunction(field1_dum, field2_dum):
if field1_dum >= 1 or field2_dum >= 3:
return -3
elif field1_dum == 0 and (field2_dum==1 or field2_dum==2):
return -2
elif field1_dum == 0 and field2_dum < 3 and mone >= 5:
return -2
if field1_dum == 0 and field2_dum == 0 and mone ==4:
return -1"""

expression = 'myFunction(!{0}!, !{1}!)'.format(field1,field2)

arcpy.CalculateField_management(fc,"EPK_Gesamt",expression,"PYTHON_9.3",codeblock)
``````

I've created a script tool with this code only and two input params of String types. The field names strings I submit are being inserted into the expression correctly.

As of personal preference, consider using the arcpy cursors for updating the fields instead of running the GP tool.

• That works perfect! Aug 26, 2015 at 9:57

I have one more question regarding that issue:

``````>>> test1 = "test1"
>>> fieldn = '!{}!'.format(test1)
>>> print fieldn
!test1!

fieldna = '(!{0}!, !{1}!)'.format(test1,test2)
>>> print fieldna
(!test1!, !test2!)
``````

Now I do see a difference. If I use the format funtion I get a tuple, but the format fits anyway. I don't get why I cannot use the first version in my function like

``````expression = 'myFunction(test1,test2)'
``````

It works fine with :

``````expression = 'myFunction(!{0}!, !{1}!)'.format(test1,test2)
``````

But I don't understand why