Error message in script which worked correctly days before? Any ideas why?
line 16, in <module>
quercy = field + "=" + "'" + Loop1 + "'"
TypeError: cannot concatenate 'str' and 'int' objects
import arcpy import os input = arcpy.GetParameterAsText(0) clipfeature = arcpy.GetParameterAsText(1) output = arcpy.GetParameterAsText(2) clipfeature_Layer = "clipfeature_Layer" arcpy.env.workspace = output arcpy.env.overwriteOutput = True field = "BFN_NR" inputSp = input.split(";") arcpy.MakeFeatureLayer_management(clipfeature, clipfeature_Layer, "", "", "") cursor = arcpy.SearchCursor(clipfeature) for row in cursor: arcpy.AddMessage(row.getValue(field)) Loop1 = row.getValue(field) quercy = field + "=" + "'" + Loop1 + "'" arcpy.SelectLayerByAttribute_management (clipfeature_Layer, "NEW_SELECTION", quercy) Sel_shape = "Sel_" + Loop1 + ".shp" arcpy.CopyFeatures_management(clipfeature_Layer, Sel_shape) for i in inputSp: arcpy.AddMessage(i) arcpy.Clip_analysis(i, Sel_shape, "Clip_" + row.getValue(field) + "_" + i)
Solved! Go to Solution.
quercy = field + "=" + "'""'" + str(Loop1) + "'"
just to ensure you have string values in case Loop1 returns a number
quercy = field + "=" + "'""'" + str(Loop1) + "'"
just to ensure you have string values in case Loop1 returns a number
you could also try formatting it like this:
Loop1 = "somestring" field = "BFN_NR" quercy = ("{0} = '{1}'".format(field, Loop1)) print(quercy) # returns... BFN_NR = 'somestring'
that's how I usually do it anyway (and surprised Dan didn't mention that )
Attribute field must be a string defined field and it's all right
Doen't work with integer or number fields, obviously