Problem with clip script wich loops throught attributes of clipping layer

1562
4
Jump to solution
07-20-2016 10:13 PM
JohannesBierer
Regular Contributor

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) 
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

quercy = field + "=" + "'""'" + str(Loop1) + "'"

just to ensure you have string values in case Loop1 returns a number

View solution in original post

4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

quercy = field + "=" + "'""'" + str(Loop1) + "'"

just to ensure you have string values in case Loop1 returns a number

RebeccaStrauch__GISP
MVP Esteemed Contributor

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 )

JohannesBierer
Regular Contributor

Attribute field must be a string defined field and it's all right

0 Kudos
JohannesBierer
Regular Contributor

Doen't work with integer or number fields, obviously

0 Kudos