5

I am trying to perform a query based on values in a Python list. The where clause will then be used in an Update Cursor, so that the rows NOT equal to the values in the list will be deleted.

This is what the list will look like after it is passed into the script from the user (using a multivalue parameter in the script tool), and the semicolon delimited string has been split. This is just an example of many choices the user can make.

userSelectionList = ["40","77","95]
selectionQRY = "RTE_1_NBR <> 'userSelectionList'" # <-- I know this doesn't work

Hard coded, it would look like:

selectionQRY = "RTE_1_NBR <> '40' AND RTE_1_NBR <> '77' AND RTE_1_NBR <> '95'"

But the input and number of selection depends on the user's selection.

It should finally be something like this:

userSelectionList = ["40","77","95]
selectionQRY = "RTE_1_NBR <> 'userSelectionList'"
with arcpy.da.UpdateCursor(userI,["RTE_1_NBR"],selectionQRY) as userUC:
 for row in userUC:
 userUC.deleteRow()
del userUC

Is it possible to make the where clause from a user input/dynamic Python list?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 13, 2013 at 18:40

3 Answers 3

7

You can use the join method:

selectionQRY = "RTE_1_NBR <> '" + "' AND RTE_1_NBR <> '".join(userSelectionList) + "'"

A string will be created, in three parts. The middle part is the key, where code of the form string.join(list) will take all objects in the list, convert them to strings, and then append them all together, with the specified string interjected between each of them. So, ",".join(["a","b","c"]) would produce "a,b,c".

In your particular example, the code "' AND RTE_1_NBR <> '".join(userSelectionList) would produce the string 40' AND RTE_1_NBR <> '77' AND RTE_1_NBR <> '95 for userSelectionList = ["40","77","95"]. Obviously, the opening RTE_1_NBR <> ' and closing ' are missing from the query, so those need to be added separately, at the beginning and end, respectively.

answered Apr 13, 2013 at 19:24
0
4

You could modify a function given in one of my previous answers which builds a where clause from a list of values and automatically adds the correct field delimiters and wraps string values in single-quotes. Just change IN to NOT IN to do the negation.

The resulting where clause would look something like "RTE_1_NBR" NOT IN('40','77','95'), depending on the underlying DBMS and field type.

answered Apr 13, 2013 at 20:36
0
0

How about using a for loop to build the query.

selectionQRY = "\"RTE_1_NBR\" <> \'" + userSelectionList[0] + "\'"
for selection in userSelectionList[1:]:
 selectionQRY += " AND \"RTE_1_NBR\" <> \'" + selection + "\'"
answered Apr 13, 2013 at 19:34

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.