Arcpy CalculateField Unexpected Indent Error

245
5
Jump to solution
01-23-2020 07:53 AM
Highlighted
New Contributor III

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.

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Esteemed Contributor
fields=[["total_pop_intersect", "!total_pop* (!Shape_Area!/!tract_area!)"]

are you missing an ! mark ?

"!total_pop! * 

View solution in original post

5 Replies
Highlighted
MVP Esteemed Contributor
fields=[["total_pop_intersect", "!total_pop* (!Shape_Area!/!tract_area!)"]

are you missing an ! mark ?

"!total_pop! * 

View solution in original post

Highlighted
New Contributor III

I was missing that ! mark in the code I pasted here, but with that correction I still receive the same error.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

fields=[["t....

try

fields=([["t...

you are also missing a leading (

Examine this to make sure it is right

fields
Out[14]:
[['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!)']]
Highlighted
New Contributor III

Dan_Patterson ,

I feel quite foolish now because after digging into this a little deeper, I realized I was using the alias instead of the field name. Normally, that would not be a problem but because I joined two tables together, it placed the table name in front of the field name. I was able to go in an add the correct field name with the table name in front of it and everything ran fine. Thanks for working on this with me. 

-Miguel

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Please mark one of the responses as Correct to close out the thread.