0

I have a table. Based of field name "TYP", I would like to select rows which contains string values "a" or "b". I need to specify the query to my arcpy code.

fc = "C:\\Users\\type_jan18.shp"
field = "TYP"
val1 = "a"
val2 = "b"

I understand how to build expression if I have one condition:

# select by one condition
whereClause = '"TYP"' + " = '" + str(val1) + "'"
# by using FieldDelimiters, again one condition
whereClause = """{} = str(val1) """.format(arcpy.AddFieldDelimiters(fc, field))

But how to correctly state it with the OR statement?

This one seems not working...

 whereClause = field + " = '" + val1 + "'"+ "or" + field + " = '" + val2 + "'"
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 22, 2018 at 9:09
0

1 Answer 1

2

Dont use +-signs, instead use format like you did with one condition. Format can take more arguments than two:

sql = """{0}='{1}' OR {0}='{2}'""".format(arcpy.AddFieldDelimiters(fc, field), val1 ,val2)

Which will give the same results as:

sql = """{0} IN('{1}', '{2}')""".format(arcpy.AddFieldDelimiters(fc, field), val1, val2)

And if you have more variables than format can handle you can place them inside a tuple:

sql = """{0} IN{1}""".format(arcpy.AddFieldDelimiters(fc, field), (val1, val2,...,val9999))
answered Feb 22, 2018 at 9:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.