I have point features being selected by a polygon feature and copied to their own shapefile. I also have certain values of the polygon feature being applied to the newly created point file. The point file already has the required fields that need to be populated. The integer/floating fields are copying correctly, but the lone string field (SfcType)/row[4] value isn't being applied. I realize this has to do with the fact that it is a string, but I can't seem to find the right syntax.

Can anyone point out my (obvious) mistake?

from arcpy import * import os
arcpy.env.overwriteOutput = True

#Make feature layer of feature classes to allow selection
MakeFeatureLayer_management (inPolyfc, "Polylyr")
MakeFeatureLayer_management (inPointfc, "Pointlyr")

#Get name of OID field for sql expression
polyOIDfld = Describe ("Polylyr").OIDFieldName

#iterate through polygons with cursor
cursor = da.SearchCursor ("Polylyr", ["OID@", PolyIDfld, "Slope", "endelev", "SfcType", "NearID"])
for row in cursor:
#sql to select individual polygon
sql = '"{0}" = {1}'.format (polyOIDfld, str(row[0]))
    #select individual polygon
SelectLayerByAttribute_management ("Polylyr", "", sql)

#select intersecting points
SelectLayerByLocation_management ("Pointlyr", "", "Polylyr")

#Check to make sure points are selected, ie at least one point actually intersects polygon
if Describe ("pointlyr").FIDSet:
    FeatureClassToFeatureClass_conversion ("Pointlyr", outPath, str(row[1]))
    print "Created Shapefile Named: " + str(row[1])
    Fc = outPath + "/"+ str(row[1]) + ".shp"
    arcpy.CalculateField_management (Fc, "Slope", str(row[2]))
    print "Added SLope to " + Fc
    Fc = outPath+ "/" + str(row[1]) + ".shp"
    arcpy.CalculateField_management (Fc, "endelev", str(row[3]))
    print "Added Rwy End Elev to " + Fc
    Fc = outPath+ "/" + str(row[1]) + ".shp"
    arcpy.CalculateField_management (Fc, "SfcType", str(row[4]))
    print "Added Sfc Type to " + Fc
    Fc = outPath+ "/" + str(row[1]) + ".shp"
    arcpy.CalculateField_management (Fc, "NearID", str(row[5]))
    print "Added NearID to " + Fc
    #Create empty feature class if no points intersect
    print "No points were under the " + str(row[1]) + " surface"

The error is occurring because you do not quote the value properly. To send a string to the field calculator using a script you have to send a quoted string with double quote characters in the string, '"The Brown Dog"'.

So I believe the proper syntax for that line is:

arcpy.CalculateField_management (Fc, "SfcType", '"{0}"'.format(str(row[4])))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.