I have a lines shapefile. I made a small code to select some of its elements and turn them into a standalone layer. The code is like this:

arcpy.env.workspace = r"C:\MyDirectory\Mygdb.gdb"
network = r"Linesshapefile"
arcpy.MakeFeatureLayer_management(network,'Winners_lyr', "OBJECTID_12" in Winners)

The last line is because, according to the documentation, the layer is not saved but only locally created, since I want to be able to access it also after the code.

Winners is an list containing integer numbers, like this:

Winners = [1, 4, 59, 330]

ObjectID_12is the code that univocally identifies my elements ( I think it received the appendix _12 because this code is the result of several geoprecessing actions, including intersections)

when I run my code, anyway, I get this error:

 File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\management.py", line 6477, in MakeFeatureLayer
raise e
arcgisscripting.ExecuteError: ERROR 000358: Invalid expression 0
Failed to execute (MakeFeatureLayer).

I guess the issue is the SQL condition I wrote, but I am not sure how to fix it since the MakeFeatureLayer_management is not very generous of information concerning how to use this function. How can I solve the problem?


Since Winners is a list of integers, and MakeFeatureLayer is expecting a SQL string in a particular format, you need to convert. Here's a simple way:

>>> Winners = [1, 4, 59, 330]
>>> sql = "OBJECTID_12 IN ({})".format(",".join(map(str, Winners)))
>>> print(sql)
'OBJECTID_12 IN (1,4,59,330)'

Since your values are integers, you need to cast to strings before joining with a comma.

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.