AnsweredAssumed Answered

Arcpy CalculateField Unexpected Indent Error

Question asked by miguelf88 on Jan 23, 2020
Latest reply on Jan 23, 2020 by bixb0012

I am trying to write a Python script that adds several fields to a feature class and then calculates those new fields. This is a process I will have to do to multiple layers so I am hoping to automate this. I am using ArcGIS Pro 2.4.3 and Python 3.7. I have the following script:

# Import system modules
import arcpy

# Set environment settings
arcpy.env.workspace = r"D:\Project Data\System Analysis\Jan 2020\Sys_analysis_2020\Sys_analysis_2020.gdb"
table = "single_market_area_no_urban_working_intersect"

# Add Fields
arcpy.AddField_management(in_table=table,
                          field_name="total_pop_intersect",
                          field_type="SHORT")
arcpy.AddField_management(in_table=table,
                          field_name="white_only_pop_intersect",
                          field_type="SHORT")
arcpy.AddField_management(in_table=table,
                          field_name="no_vehicle_pop_intersect",
                          field_type="SHORT")
arcpy.AddField_management(in_table=table,
                          field_name="under18_pop_intersect",
                          field_type="SHORT")
arcpy.AddField_management(in_table=table,
                          field_name="over65_pop_intersect",
                          field_type="SHORT")
arcpy.AddField_management(in_table=table,
                          field_name="poverty_pop_intersect",
                          field_type="SHORT")
arcpy.AddField_management(in_table=table,
                          field_name="total_hh_intersect",
                          field_type="SHORT")
arcpy.AddField_management(in_table=table,
                          field_name="minority_pop_intersect",
                          field_type="SHORT")

# Calculate newly added fields
arcpy.CalculateFields_management(in_table=table,
                                 expression_type="PYTHON3",
                                 fields=[["total_pop_intersect", "!total_pop* (!Shape_Area!/!tract_area!)"],
                                         ["white_only_pop_intersect", "!white_only_pop! * (!Shape_Area!/!tract_area!)"],
                                         ["no_vehicle_pop_intersect", "!no_vehicle_pop! * (!Shape_Area!/!tract_area!)"],
                                         ["under18_pop_intersect", '!under18_pop! * (!Shape_Area!/!tract_area!)'],
                                         ["over65_pop_intersect", "!65over_pop! * (!Shape_Area!/!tract_area!)"],
                                         ["poverty_pop_intersect", "!poverty_pop! * (!Shape_Area!/!tract_area!)"],
                                         ["total_hh_intersect", "!total_households! * (!Shape_Area!/!tract_area!)"],
                                         ["minority_pop_intersect", "!minority_pop! * (!Shape_Area!/!tract_area!)"]])

When I run the code above, I receive multiple lines of error messages:

Traceback (most recent call last):
  File "<string>", line 44, in <module>
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 4265, in CalculateFields
    raise e
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 4262, in CalculateFields
    retval = convertArcObjectToPythonObject(gp.CalculateFields_management(*gp_fixargs((in_table, expression_type, fields, code_block), True)))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000539: Invalid field total_pop
ERROR 000539: Invalid field white_only_pop
ERROR 000539: Invalid field no_vehicle_pop
ERROR 000539: Invalid field under18_pop
ERROR 000539: Invalid field 65over_pop
ERROR 000539: Invalid field poverty_pop
ERROR 000539: Invalid field total_households
ERROR 000539: Invalid field minority_pop
ERROR 000539:   File "<expression>", line 1
    * (/1)
    ^
IndentationError: unexpected indent

ERROR 000539:   File "<expression>", line 1
    * (/2)
    ^
IndentationError: unexpected indent

ERROR 000539:   File "<expression>", line 1
    * (/3)
    ^
IndentationError: unexpected indent

ERROR 000539:   File "<expression>", line 1
    * (/4)
    ^
IndentationError: unexpected indent

ERROR 000539:   File "<expression>", line 1
    * (/5)

The error message goes on like that for 1000 lines. 

 

I have been able to run the first half the code by itself and create the new fields with no errors. When I try to run the last half by itself, I receive the same error message as above.

I have tried to just calculate one field using Python and I still receive the same error.

arcpy.CalculateFields_management(in_table=table, expression_type="PYTHON3",
                                 fields=[["total_pop_intersect", "!total_pop! * (!Shape_Area!/!tract_area!)"]])

 Where am I going wrong? Any guidance would be appreciated.

Outcomes