0

I have a problem in using the Select_analysis or Select by attribute, what I need t do, is to extract all the residential uses from a land use feature class, and make a feature class for residential, but there are more than one value for residential uses like (''Residential and driving school'', ''mixed commercial Residential'', ''mixed residential with other uses'', 'residential') As I use the following script:

uses_FieldForLandUses= arcpy.GetParameterAsText(4) # field of land use
Values_of_Residential_uses_in_Land_use_field= arcpy.GetParameterAsText(5) # type: string (multi value)- all the values for residential
Output_Residential_uses_parcels= arcpy.GetParameterAsText(6) # type: feature class    
#Variables for residential uses
valueList = [x.strip() for x in Values_of_Residential_uses_in_Land_use_field.split(";")]

FieldInfo = arcpy.ListFields(inputLandUseParcels_FeatureClass,uses_FieldForLandUses)[0]
fieldType = FieldInfo.type


if uses_FieldForLandUses  == '#' or not uses_FieldForLandUses:
    Field = "LAND_USE" # provide a default value if unspecified

# Process: Select

if fieldType == 'String':
    where = "{} IN ('{}')".format(arcpy.AddFieldDelimiters(inputLandUseParcels_FeatureClass,uses_FieldForLandUses), "', '".join(valueList))


arcpy.Select_analysis(inputLandUseParcels_FeatureClass, Output_Residential_uses_parcels, where)

but when selecting more than one value for residential, I get the following error:

ExecuteError: ERROR 000358: Invalid expression land_use IN (''Residential and driving school'', ''mixed commercial Residential'', ''mixed residential with other uses'', 'residential')
Failed to execute (Select).
  • Please format your code legibly by using the {} button to prepend four spaces on each line. – Vince Jul 21 '16 at 11:36
1

In the result the expression it has returned two single quotes, it should be or with one single quote or with double quote. I suggest to try it:

where = "{} in ({})".......

Also do not forget if the source is .gdb Expression should be:

**Fieldname = 'value'**

if the source is .mdb: Expression is:

**[Fieldname] = value**

And if it is shapefile: Expression is:

**"Fieldname" = 'value'**

If you respect the expression for each type of input it should work.

  • thanks for the answer, but where should I write the expression of Fieldname = 'value'?? – N.G Jul 21 '16 at 13:31
  • Also I have tried this: – N.G Jul 21 '16 at 13:40
  • where = "{} in ({})".format(arcpy.AddFieldDelimiters(inputLandUseParcels_FeatureClass,uses_FieldForLandUses), "', '".join(valueList)) – N.G Jul 21 '16 at 13:40
  • as suggested but have the same error! – N.G Jul 21 '16 at 13:41
0

First of all: write a line before select_analysis: print where Just to view what "where clause" returns.

Than try to convert it in specific expressions: For example: If your feature class "inputLandUseParcels_FeatureClass" is shapefile, than "where" should be:

'"Fieldname" in ("value1","value2","value3")'

if the input feature is inside ".gdb" or ".mdb" see the example in first reply.

  • I have modified my code as follows – N.G Jul 21 '16 at 14:13
  • uses_FieldForLandUses in '(valueList)' # Process: Select if fieldType == 'String': where = "{} IN ({})".format(arcpy.AddFieldDelimiters(inputLandUseParcels_FeatureClass,uses_FieldForLandUses), "', '".join(valueList)) print where arcpy.Select_analysis(inputLandUseParcels_FeatureClass, Output_Residential_uses_parcels, where) – N.G Jul 21 '16 at 14:13
  • with regards to Fieldname = 'value' expression – N.G Jul 21 '16 at 14:14
  • I have written it as uses_FieldForLandUses in '(valueList)' – N.G Jul 21 '16 at 14:15
  • because the residential values is the user input , so I can not write '"Fieldname" in ("value1","value2","value3")' because I can not expect what will be value 1 or value 2...if I have rightly understood you – N.G Jul 21 '16 at 14:16
0

enter image description here

This is what I want to do that the residential values is the user input , so I can not write '"Fieldname" in ("value1","value2","value3")'

  • I have posted the picture as an answer because I could not post it as a comment, however the problem has not been fixed – N.G Jul 21 '16 at 15:45
  • 1
    Please do not post non-answers as Answers. This information belongs in the question itself. Your assertion of inability to use the in operator is incorrect. This is an exercise in string formatting. You just need to work with print statements until the correct format is achieved. – Vince Jul 21 '16 at 20:40
0

The problem are the doubled single quotes in your where clause string. I did changes in valueList Generation and the assignment of the where variable:

uses_FieldForLandUses= arcpy.GetParameterAsText(4) # field of land use
Values_of_Residential_uses_in_Land_use_field= arcpy.GetParameterAsText(5) # type: string (multi value)- all the values for residential
Output_Residential_uses_parcels= arcpy.GetParameterAsText(6) # type: feature class    
#Variables for residential uses

if fieldType == 'String':
    valueList = ["'" + x.strip() + "'" for x in Values_of_Residential_uses_in_Land_use_field.split(";")]
else: # e.g. integer
    valueList = [x.strip() for x in Values_of_Residential_uses_in_Land_use_field.split(";")]

FieldInfo = arcpy.ListFields(inputLandUseParcels_FeatureClass,uses_FieldForLandUses)[0]
fieldType = FieldInfo.type


if uses_FieldForLandUses  == '#' or not uses_FieldForLandUses:
    Field = "LAND_USE" # provide a default value if unspecified

# Process: Select
where = "{} IN ('{}')".format(arcpy.AddFieldDelimiters(inputLandUseParcels_FeatureClass,uses_FieldForLandUses), ", ".join(valueList))

arcpy.Select_analysis(inputLandUseParcels_FeatureClass, Output_Residential_uses_parcels, where)

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.