Field Caluclator Error

1330
6
Jump to solution
03-27-2013 11:22 AM
RachelAlbritton1
New Contributor III
I've written a script. that uses the field calculator to create a value for a new field, "TotGroupField". This field value is = to Field1+Field2+Field3....

I'm getting the following error:
Traceback (most recent call last):   File RCW\Code\ForLoop.py", line 99, in <module>     GrpExpression = sum([GrpField1New+GrpField2New+GrpField3New+GrpField4New+GrpField5New]) TypeError: unsupported operand type(s) for +: 'int' and 'str'


Script:
#Create Feature Layers from Feature Classes parcelsFC = "ACUB_3mile.shp" parcelsFL = outName(parcelsFC,"_Layer") CbuffersFL = outName(CBuffersFC,"_Layer")  arcpy.MakeFeatureLayer_management(parcelsFC, parcelsFL) arcpy.MakeFeatureLayer_management(CBuffersFC, CbuffersFL)  TotalGrpField = "TotGrpCalc" #Temp - will be deleted TotalPBGField = "TotaBGCalc" #Temp - Will be deleted AvgGrpField= "AvgGrp0812" #Make into user input AvgPBGField= "AvgPBG0812" #Make into user input arcpy.AddField_management(CbuffersFL,TotalGrpField,"DOUBLE") print arcpy.GetMessages(),"\n" arcpy.AddField_management(CbuffersFL,TotalPBGField,"DOUBLE") print arcpy.GetMessages(),"\n" arcpy.AddField_management(CbuffersFL,AvgGrpField, "DOUBLE") print arcpy.GetMessages(),"\n" arcpy.AddField_management(CbuffersFL,AvgPBGField,"DOUBLE") print arcpy.GetMessages(),"\n"  #Join All Nest Files Table to new selected buffer file. This table was preproccessed and made into a single dbf table (joined by cluster key) showing groupsize and PBG by year. #A column was created called TotalSize and Total PGB for each cluster. The total column will be used to help calculate the average group size/average pbg size for RCW clusters #whose 3-mile dispersal buffer intersect with each parcel.  #This means this table will have to be updated manually every year unless written into the script. #This join is used to calculate criteria 2 and 3. #Input for a join must be a layer  NestFiles="/RCW/Nest Data/Edited Nest Data/Nest_All.dbf" arcpy.AddJoin_management(CbuffersFL,"Cluster_ke",NestFiles,"Key12","KEEP_ALL") print arcpy.GetMessages(),"\n"   GrpField1= "Nest_All.GrpSz08" #User selects field GrpField2= "Nest_All.GrpSz09" #User selects field GrpField3= "Nest_All.GrpSz10" #User selects field GrpField4= "Nest_All.GrpSz11" #User Selects field GrpField5= "Nest_All.GrpSz12" #User selects field GrpField1New= "!"+GrpField1+"!" GrpField2New= "!"+GrpField2+"!" GrpField3New= "!"+GrpField3+"!" GrpField4New= "!"+GrpField4+"!" GrpField5New= "!"+GrpField5+"!"  GrpExpression = sum([GrpField1New+GrpField2New+GrpField3New+GrpField4New+GrpField5New])    arcpy.CalculateField_management(CbuffersFL,TotalGrpField,GrpExpression,"PYTHON") print arcpy.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RachelAlbritton1
New Contributor III
I figured this issue out. i need to add the + sign into the variable so that the script looks like this:

GrpField1= "Nest_All.GrpSz08" #User selects field GrpField2= "Nest_All.GrpSz09" #User selects field GrpField3= "Nest_All.GrpSz10" #User selects field GrpField4= "Nest_All.GrpSz11" #User Selects field GrpField5= "Nest_All.GrpSz12" #User selects field GrpField1New= '!'+GrpField1+'!+' GrpField2New= '!'+GrpField2+'!+' GrpField3New= '!'+GrpField3+'!+' GrpField4New= '!'+GrpField4+'!+' GrpField5New= '!'+GrpField5+'!'  GrpExpression = (GrpField1New+GrpField2New+GrpField3New+GrpField4New+GrpField5New)


But I'm also having a similar issue with this calculation
#Get count of how many records are selected. This calculation is used to calculate avg group size and avg pbg size over the past five yrs.     SelectedCount=int(arcpy.GetCount_management(CbuffersFL).getOutput(0))     print "\nThere are",SelectedCount, "buffers that intersect with parcel", FID      #calculate the avg group size for selected groups. This avgerage will be temporarily recorded into the GrpAvg0812 Field located within CbuffersFL.     #Once calculated an update cursor will be used to record this average in the selected parcels shapefile.     GrpFieldJoin=CbuffersFL[:-6]+"."+TotalGrpField     GrpFieldJoinNew='!'+GrpFieldJoin+'!/'     GrpExpression=(GrpFieldJoinNew)/SelectedCount)


I know the issue is that GrpFieldJoinNew is a string but if I code the expression to read
GrpExpression=(int(GrpFieldJoinNew)/SelectCount)


I get this message
ValueError: invalid literal for int() with base 10: '!RCW_Cluster_2010_3miBuff.TotGrpCalc!/'


I'm not sure how I turn this string into an interger value

View solution in original post

0 Kudos
6 Replies
JamesCrandall
MVP Frequent Contributor
One of these fields is type Text or non-numeric

sum([GrpField1New+GrpField2New+GrpField3New+GrpField4New+GrpField5New])
0 Kudos
RachelAlbritton1
New Contributor III
OK, so based on this article, I replaced the double quotes with single quotes. The output message of the tool now says that its running successfully, but when I use a search cursor to get the values of the rows, the values are incorrect.

This is leading me to believe that even though the arcpy module is saying the tool is successfully calculating these fields, there is still an issue with formatting.  Thoughts?

New Code:
TotalGrpField = "TotGrpCalc" #Temp - will be deleted
TotalPBGField = "TotaBGCalc" #Temp - Will be deleted
AvgGrpField= "AvgGrp0812" #Make into user input
AvgPBGField= "AvgPBG0812" #Make into user input
arcpy.AddField_management(CbuffersFL,TotalGrpField,"DOUBLE")
print arcpy.GetMessages(),"\n"
arcpy.AddField_management(CbuffersFL,TotalPBGField,"DOUBLE")
print arcpy.GetMessages(),"\n"
arcpy.AddField_management(CbuffersFL,AvgGrpField, "DOUBLE")
print arcpy.GetMessages(),"\n"
arcpy.AddField_management(CbuffersFL,AvgPBGField,"DOUBLE")
print arcpy.GetMessages(),"\n"

#Join All Nest Files Table to new selected buffer file. This table was preproccessed and made into a single dbf table (joined by cluster key) showing groupsize and PBG by year.
#A column was created called TotalSize and Total PGB for each cluster. The total column will be used to help calculate the average group size/average pbg size for RCW clusters
#whose 3-mile dispersal buffer intersect with each parcel. 
#This means this table will have to be updated manually every year unless written into the script.
#This join is used to calculate criteria 2 and 3.

#Input for a join MUST BE A LAYER

NestFiles="/RCW/Nest Data/Edited Nest Data/Nest_All.dbf"
arcpy.AddJoin_management(CbuffersFL,"Cluster_ke",NestFiles,"Key12","KEEP_ALL")
print arcpy.GetMessages(),"\n"


GrpField1= "Nest_All.GrpSz08" #User selects field
GrpField2= "Nest_All.GrpSz09" #User selects field
GrpField3= "Nest_All.GrpSz10" #User selects field
GrpField4= "Nest_All.GrpSz11" #User Selects field
GrpField5= "Nest_All.GrpSz12" #User selects field
GrpField1New= '!'+GrpField1+'!'
GrpField2New= '!'+GrpField2+'!'
GrpField3New= '!'+GrpField3+'!'
GrpField4New= '!'+GrpField4+'!'
GrpField5New= '!'+GrpField5+'!'

GrpExpression = (GrpField1New+GrpField2New+GrpField3New+GrpField4New+GrpField5New)

arcpy.CalculateField_management(CbuffersFL,TotalGrpField,GrpExpression,"PYTHON")
print arcpy.GetMessages()


#Check to see if calculation is working
TotalGrpFieldNew=CbuffersFL[:-6]+"."+TotalGrpField
print TotalGrpFieldNew
sc = arcpy.SearchCursor(CbuffersFL)
for line in sc:
    print line.getValue(TotalGrpFieldNew)
del line
del sc
0 Kudos
RachelAlbritton1
New Contributor III
I figured this issue out. i need to add the + sign into the variable so that the script looks like this:

GrpField1= "Nest_All.GrpSz08" #User selects field GrpField2= "Nest_All.GrpSz09" #User selects field GrpField3= "Nest_All.GrpSz10" #User selects field GrpField4= "Nest_All.GrpSz11" #User Selects field GrpField5= "Nest_All.GrpSz12" #User selects field GrpField1New= '!'+GrpField1+'!+' GrpField2New= '!'+GrpField2+'!+' GrpField3New= '!'+GrpField3+'!+' GrpField4New= '!'+GrpField4+'!+' GrpField5New= '!'+GrpField5+'!'  GrpExpression = (GrpField1New+GrpField2New+GrpField3New+GrpField4New+GrpField5New)


But I'm also having a similar issue with this calculation
#Get count of how many records are selected. This calculation is used to calculate avg group size and avg pbg size over the past five yrs.     SelectedCount=int(arcpy.GetCount_management(CbuffersFL).getOutput(0))     print "\nThere are",SelectedCount, "buffers that intersect with parcel", FID      #calculate the avg group size for selected groups. This avgerage will be temporarily recorded into the GrpAvg0812 Field located within CbuffersFL.     #Once calculated an update cursor will be used to record this average in the selected parcels shapefile.     GrpFieldJoin=CbuffersFL[:-6]+"."+TotalGrpField     GrpFieldJoinNew='!'+GrpFieldJoin+'!/'     GrpExpression=(GrpFieldJoinNew)/SelectedCount)


I know the issue is that GrpFieldJoinNew is a string but if I code the expression to read
GrpExpression=(int(GrpFieldJoinNew)/SelectCount)


I get this message
ValueError: invalid literal for int() with base 10: '!RCW_Cluster_2010_3miBuff.TotGrpCalc!/'


I'm not sure how I turn this string into an interger value
0 Kudos
RachelAlbritton1
New Contributor III
I am trying to divide field values by an integer using field calculator. The integer value is being derived from a arcpy.GetCount statement and the fielded is in a joined table wherein the field itself is part of the origin table (I know you can't use field calculator on the joined table).

for parcel in range(0,1092):
    FID = "FID=%s" % (parcel)
    arcpy.SelectLayerByAttribute_management(SelectedParcelsFL,"NEW_SELECTION",FID)
    arcpy.SelectLayerByLocation_management(CbuffersFL,"INTERSECT",SelectedParcelsFL)

    #Get count of how many records are selected. This calculation is used to calculate avg group size and avg pbg size over the past five yrs.
    SelectedCount=int(arcpy.GetCount_management(CbuffersFL).getOutput(0))
    print "\nThere are",SelectedCount, "buffers that intersect with parcel", FID

    #calculate the avg group size for selected groups. This avgerage will be temporarily recorded into the GrpAvg0812 Field located within CbuffersFL.
    #Once calculated an update cursor will be used to record this average in the selected parcels shapefile.
    GrpFieldJoin=CbuffersFL[:-6]+"."+TotalGrpField
    GrpFieldJoinNew='!'+GrpFieldJoin+'!'
    arcpy.AddMessage("Calculating average group size over the last 5 years for RCW Clusters whose 3-mile dispersal buffer intercects each parcel")
    arcpy.CalculateField_management(CbuffersFL,AvgGrpField,(GrpFieldJoinNew/SelectedCount),"PYTHON")



I get the following error
TypeError: unsupported operand type(s) for /: 'str' and 'int'


I'm understanding this to mean that the GrpJoinFieldNew is a string, but if I try to structure it as an integer
(int(GrpFieldJoinNew)/SelectedCount)


I get this:
ValueError: invalid literal for int() with base 10: '!RCW_Cluster_2010_3miBuff.TotGrpCalc!'
0 Kudos
DanPatterson_Retired
MVP Emeritus
the field you are trying is a text/string field and it is probably left justified or it contains null values, float the field value then do the division
0 Kudos
curtvprice
MVP Esteemed Contributor

    arcpy.CalculateField_management(CbuffersFL,AvgGrpField,(GrpFieldJoinNew/SelectedCount),"PYTHON")


The above calculate expression is not properly formed. The divide symbol must be inside the expression string. (This is the same solution you posted earlier today -- in fact I merged the threads here.)

Here's a way to approach building the expression using Python string formatting -- generally far easier to debug than adding strings together the way you've been doing it:

SelectedCount=int(arcpy.GetCount_management(CbuffersFL).getOutput(0))
# the name is used to prefix the field name
prefix = arcpy.Describe(CbuffersFL).name 
expr = "!{0}.{1}! / float({2})".format(prefix, TotalGrpField, SelectedCount)
arcpy.CalculateField_management(CbuffersFL, AvgGrpField, expr, "PYTHON_9.3")


Here's how using the python formatting simplifies your field addition calculation:

GrpField1= "Nest_All.GrpSz08" 
GrpField2= "Nest_All.GrpSz09"
GrpField3= "Nest_All.GrpSz10" 
GrpField4= "Nest_All.GrpSz11" 
GrpField5= "Nest_All.GrpSz12" 
fmt = "!{0}! + !{1}! + !{2}! + !{3}! + !{4}! + !{5}!" 
Grpexpression = fmt.format(GrpField1New, GrpField2New, \
                   GrpField3New, GrpField4New, GrpField5New)


Python also has a sum function for lists, so you could have done this too:

fmt = "sum([!{0}!, !{1}!, !{2}!, !{3}!, !{4}!, !{5}!])" 
Grpexpression = fmt.format(GrpField1New, GrpField2New, \
                   GrpField3New, GrpField4New, GrpField5New)


Hope this helps you out.
0 Kudos