I want to use the following expression in the arcpy.CalculateField_management tool.

I have a variable called "value" and a field called "Field01"

I want to use (value - Field01) / 20

I can´t seem to find the correct syntax with arcpy.CalculateField_management.

arcpy.CalculateField_management (fc, "test",expression, "PYTHON")

Given error is: TypeError: cannot concatenate 'str' and 'int' objects

I have tried the following and many other combinations:

expression = value - "!XKoordInt!" / 20

expression = "("+value+"-!XKoordInt!)/20")"
  • ValueError: invalid literal for int() with base 10: '!XKoordInt!' – Robert Buckley Jun 13 '16 at 11:27
  • XKoordInt = Long integer e.g. 540528 – Robert Buckley Jun 13 '16 at 11:34
  • I just to know the syntax to substract a field from a variable divided by an integer – Robert Buckley Jun 13 '16 at 11:39
  • Please do not use comments to add content to your question. Instead edit the question to include this information. – Vince Jun 13 '16 at 12:41

Your second listed expression is close:

expression = "({0} - !XKoordInt!)/20".format(value)

Using the format method is generally better than string concatenation, it will take care of different input types and formatting.

| improve this answer | |

Use the format method to put the value into the expression. Using triple quotes will make it easier to have quotes inside the expression, too.

value = 5000
expression = '''({0} - !XKoord!) / 20'''.format(value)

arcpy.CalculateField_management(in_table=fc, field='test',
                                expression=expression, expression_type='PYTHON_9.3')
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.