Select to view content in your preferred language

Arcpy CalculateField Unexpected Indent Error

3775
6
Jump to solution
01-23-2020 07:53 AM
Miguel-Fernandez
Occasional Contributor

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.

Miguel Fernandez
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus
fields=[["total_pop_intersect", "!total_pop* (!Shape_Area!/!tract_area!)"]

are you missing an ! mark ?

"!total_pop! * 

View solution in original post

6 Replies
DanPatterson_Retired
MVP Emeritus
fields=[["total_pop_intersect", "!total_pop* (!Shape_Area!/!tract_area!)"]

are you missing an ! mark ?

"!total_pop! * 
Miguel-Fernandez
Occasional Contributor

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

Miguel Fernandez
0 Kudos
DanPatterson_Retired
MVP Emeritus

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!)']]
Miguel-Fernandez
Occasional Contributor

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

Miguel Fernandez
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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

fillermark
New Contributor

As the error message indicates, you have an unexpected indent error . This error occurs when a statement is unnecessarily indented or its indentation does not match the indentation of former statements in the same block. Python not only insists on indentation, it insists on consistent indentation . You are free to choose the number of spaces of indentation to use, but you then need to stick with it. If you indent one line by 4 spaces, but then indent the next by 2 (or 5, or 10, or ...), you'll get this error.

However, by default, mixing tabs and spaces is still allowed in Python 2 , but it is highly recommended not to use this "feature". Python 3 disallows mixing the use of tabs and spaces for indentation. Replacing tabs with 4 spaces is the recommended approach for writing Python code .

 

0 Kudos