Select to view content in your preferred language

Calculate Field and Python error 000539

3294
2
01-10-2012 07:20 AM
EdwardBriggler
Deactivated User
Good Day,

I am trying to add fields together and put the output in a new field. I want it hard coded in. But I keep getting an error message
[HTML]ERROR 000539: Error running expression:  +  +  <type 'exceptions.IndentationError'>: unexpected indent (<string>, line 1)
Failed to execute (CalculateField).

Failed to execute (FieldCalculator).[/HTML]

I have tried multiple ways to get it to do it and I am stuck.
My objective is to calculate the fields out by phase, then add them together to just
get single, two and three phase line.  Any help would be GREAT!!

Here is the code.
# Author:  Edward Briggler
# Date: 1/4/2012
# Purpose:  To add the 7 phase fields to a feature class and calculate based on selecting by subtype.
#           Sum by subtype, divide by 5280 to get length by 

# Bring in Arc and Sys. 
import arcgisscripting, sys

#  Create the Geoprocessor...
gp = arcgisscripting.create()

gp.Workspace = gp.GetParameterAsText(0)

fc = gp.GetParameterAsText(1)

f = gp.GetParameterAsText(2)
# concatenate the bangs to let the equations know it is a field
new_f = '!'+f+'!'

# only held in memory but name needs changing.
tv = "table_view_memory"

# expressions to evaluate the length of Primary Line (in the length of specified field)
## Involves concatenating the *2 and *3 to get the expression right. HAS TO BE A STRING!!!
exp = new_f
exp2 = new_f+'*2'
exp3 = new_f+'*3'



exp4 = "!A_PHASE! + !B_PHASE! + !C_PHASE!"
exp5 = "!AB_PHASE! + !AC_PHASE! + !BC_PHASE!"
exp6 = "ABC_PHASE"
# Turn Feature Class into a layer...can only do the following geoprocesses while a layer or table view.
gp.MakeFeatureLayer_management(fc, tv)

# Add Fields to Layer
gp.addfield_management(tv, "A_PHASE", "FLOAT")
gp.addfield_management(tv, "B_PHASE", "FLOAT")
gp.addfield_management(tv, "C_PHASE", "FLOAT")
gp.addfield_management(tv, "AB_PHASE", "FLOAT")
gp.addfield_management(tv, "AC_PHASE", "FLOAT")
gp.addfield_management(tv, "BC_PHASE", "FLOAT")
gp.addfield_management(tv, "ABC_PHASE", "FLOAT")

# Final Fields...
gp.addfield_management(tv, "SINGLE_PH", "FLOAT")
gp.addfield_management(tv, "TWO_PH", "FLOAT")
gp.addfield_management(tv, "THREE_PH", "FLOAT")



# Select by SUBTYPECD and run each expression accordingly

gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 1")
gp.CalculateField_management(tv, "A_PHASE",exp , "PYTHON" )
gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 2")
gp.CalculateField_management(tv, "B_PHASE",exp , "PYTHON" )
gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 3")
gp.CalculateField_management(tv, "C_PHASE",exp , "PYTHON" )
gp.SelectLayerByAttribute_management(tv,  "NEW_SELECTION", "SUBTYPECD = 4")
gp.CalculateField_management(tv, "AB_PHASE",exp2 , "PYTHON" )
gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 5")
gp.CalculateField_management(tv, "AC_PHASE",exp2 , "PYTHON" )
gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 6")
gp.CalculateField_management(tv, "BC_PHASE",exp2 , "PYTHON" )
gp.SelectLayerByAttribute_management(tv, "NEW_SELECTION", "SUBTYPECD = 7")
gp.CalculateField_management(tv, "ABC_PHASE",exp3 , "PYTHON" )

# take each subtype, sum together and divide by 5280 to get total length in miles by single, two and three
# phase line.
gp.CalculateField_management(tv, "SINGLE_PH", exp4, "PYTHON" )
gp.CalculateField_management(tv, "TWO_PH", exp5, "PYTHON" )
gp.CalculateField_management(tv, "THREE_PH", exp6, "PYTHON" )

# Delete A - ABC fields...
gp.DeleteField_management(tv,"A_PHASE; B_PHASE; C_PHASE; AB_PHASE; AC_PHASE; BC_PHASE; ABC_PHASE")
0 Kudos
2 Replies
JasonScheirer
Esri Alum
Try quoting each string in your expression, so instead of

exp4 = "!A_PHASE! + !B_PHASE! + !C_PHASE!"
exp5 = "!AB_PHASE! + !AC_PHASE! + !BC_PHASE!"


You use

exp4 = "'!A_PHASE!' + '!B_PHASE!' + '!C_PHASE!'"
exp5 = "'!AB_PHASE!' + '!AC_PHASE!' + '!BC_PHASE!'"
0 Kudos
EdwardBriggler
Deactivated User
Thanks for the reply, jscheirer!

I tried that too. but did not work.  I went ahead and used a cursor method instead.  works great!

# Author:  Edward Briggler
# Date: 1/10/2012
# Purpose:  To add 3 fields (single, two and three) phase fields, calculating the length in miles by subtype
#           using update cursor

# Bring in Arc and Sys. 
import arcgisscripting, sys

#  Create the Geoprocessor...
gp = arcgisscripting.create()

# Get Fc by parameter...
fc = sys.argv[1]





# only held in memory but name needs changing.
tv = "table_view_memor"
# Turn Feature Class into a layer...can only do the following geoprocesses while a layer or table view.
gp.MakeFeatureLayer_management(fc, tv)
# Final Fields...
gp.addfield_management(tv, "SINGLE_PH", "DOUBLE")
gp.addfield_management(tv, "TWO_PH", "DOUBLE")
gp.addfield_management(tv, "THREE_PH", "DOUBLE")

updateRows = gp.updatecursor(fc)
updateRow = updateRows.next()

while updateRow:
    if updateRow.SUBTYPECD <= 3:
        updateRow.SINGLE_PH = updateRow.Shape_Length/5280
        updateRow.TWO_PH = 0
        updateRow.THREE_PH = 0
    elif updateRow.SUBTYPECD > 3 and updateRow.SUBTYPECD < 7:
        updateRow.TWO_PH = updateRow.Shape_Length/5280
        updateRow.SINGLE_PH = 0
        updateRow.THREE_PH = 0
    elif updateRow.SUBTYPECD == 7:
        updateRow.THREE_PH = updateRow.Shape_Length/5280
        updateRow.SINGLE_PH = 0
        updateRow.TWO_PH = 0
    else:
        updateRow.SINGLE_PH = 0
        updateRow.TWO_PH = 0
        updateRow.THREE_PH = 0
    updateRows.updaterow(updateRow)
    updateRow = updateRows.next()


gp.delete(tv)
0 Kudos