Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Calculate field using variables as fields

$
0
0

When I calculate a field like this:

field_0="field_name0"
arcpy.CalculateField_management("Layer", field_0, '!field_name1! / !field_name2!', "PYTHON_9.3")

It works! The field “field_name0″ in the attribute table is populated with the value calculated from fields “field_name1″ and “field_name2″.

But, if I use variables instead of field names:

field_0="field_name0"
field_1="!field_name1!"
field_2="!field_name2!"
arcpy.CalculateField_management("Layer", field_0, 'field_1 / field_2', "PYTHON_9.3")

I get this error:

Runtime error  Traceback (most recent call last):   File "<string>", line 3, in <module>   File "c:program files (x86)arcgisdesktop10.2arcpyarcpymanagement.py", line 3354, in CalculateField     raise e ExecuteError: ERROR 000539: Error running expression: field_1/ field_2  Traceback (most recent call last):   File "<expression>", line 1, in <module> NameError: name 'field_1' is not defined  Failed to execute (CalculateField). 

I want to use loops later on, that’s why I want it to work with variables, and not simply with direct field names. Any idea would be greatly appreciated!


Viewing all articles
Browse latest Browse all 767

Trending Articles