arcpy计算字符串字段:任何想法如何解决这个问题?

时间:2014-09-29 17:05:15

标签: python arcpy calculated-field

我试图将通过元组循环获得的字符串值存储到名为ROW_1的字段中

代码就像这样

for creekclass in listOfClassTuples:

                (classname, Permanency, creekWidth, score) = creekclass
                arcpy.AddMessage(int(score))

                bufferDistance = creekWidth*0.5
                if crossingType == "INTERSECT":
                    stringBuffer = ""
                else: 
                    stringBuffer = "%s Meters" % str(bufferDistance)
                    arcpy.AddMessage(str(bufferDistance))
                arcpy.MakeFeatureLayer_management(sourceLayer,"JUST_SELECTED", fieldName +" = '"+ classname + "'")
                #arcpy.SelectLayerByAttribute_management("JUST_SELECTED","NEW_SELECTION",fieldName+" = '"+ classname + "'")
                #arcpy.SelectLayerByAttribute_management("JUST_SELECTED","SUBSET_SELECTION",fieldName2+" = '"+ Permanency + "'")
                #arcpy.CopyFeatures_management("JUST_SELECTED", "A:\Temporary\TempLayer1.shp")                           
                arcpy.SelectLayerByLocation_management(targetLayer, crossingType,
                                                       "JUST_SELECTED", stringBuffer, "NEW_SELECTION")
               ## classname = classname.lower()

                if outputField1 != "":                                        

                            arcpy.CalculateField_management(targetLayer, outputField1, classname)
                            #arcpy.AddMessage(str(classname))

                            #arcpy.AddMessage(str(outputField1))

                arcpy.CalculateField_management(targetLayer, outputField2, int(score) )


                arcpy.Delete_management("Just_selected")
                arcpy.SelectLayerByAttribute_management(targetLayer, "CLEAR_SELECTION")

    except:

        arcpy.AddMessage("Function failed")
        arcpy.AddMessage(arcpy.GetMessages())

当变量classname等于“Virtual Flow”时出现问题:

classname =“虚拟流程”

在以下代码中取自以上代码

if outputField1 != "":                                        

                            arcpy.CalculateField_management(targetLayer, outputField1, classname)

1 个答案:

答案 0 :(得分:1)

从esri帮助中的语法:

CalculateField_management (in_table, field, expression, {expression_type}, {code_block})

第三个参数是SQL表达式。由于您传递的字符串可能包含空格,因此表达式需要用单引号''包围。

这样的事情应该有效:

if outputField1 != "":                                        

                        arcpy.CalculateField_management(targetLayer, outputField1, "".join(("'",classname,"'"))