lemonpro

Calculate Field and Python error 000539

Discussion created by lemonpro on Jan 10, 2012
Latest reply on Jan 16, 2012 by lemonpro
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")

Outcomes