1

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)
arcpy.SavetoLayerFile_management('Winners_lyr','Winners_lyr') 

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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 23, 2016 at 15:08

1 Answer 1

2

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.

answered Jun 23, 2016 at 15:52

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.