2

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")"
0

2 Answers 2

2

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.

0
2

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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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