I have a shapefile that contains a text field "Company" that i would like to use to run a definition query to create a layer file. I want to be able to create a layer for each company.

I had a look around and I found Creating new layers using ArcPy to define Definition Query for each field value? which seems to detail the process.

In my code i am trying to write out a layer file for each unique value that determines a definition query. My source file contains a shapefile of shops , within the shp file there is a field that contains the company that owns the shop. I want to run a definition query on each unique company contained within the company field

i have been able to get the unique companies into a list( as pointed out , i needed to use a set to get the unique values of the search curosr) , that i would like to loop through. My code is as follows

import arcpy
from arcpy import env
# This is where the file is being picked up from 
arcpy.env.workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"
# The file to be processed 
Source_File = "SDE_COMPANY_LIST.shp"

Company_Bulk_Company_List = [row[0] for row in arcpy.da.SearchCursor(Source_File,"COMPANY")]
UniqueCompany = set(Company_Bulk_Company_List)
print UniqueCompany

# Getting the list out 

for company_query in UniqueCompany:

env.workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"

out_layer0 = str(company_query)+"lyr"
in_layer = out_layer0
out_layer = in_layer +"lyr"

#MakeFeatureLayer variables
in_features = "SDE_COMPANY_LIST.shp"

where_clause = '"COMPANY" = \'company_query\''
print where_clause
workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"

    # Execute MakeFeatureLayer
    arcpy.MakeFeatureLayer_management(in_features, out_layer0, where_clause, workspace)

    # Execute SaveToLayerFile
    arcpy.SaveToLayerFile_management(in_layer, out_layer, "ABSOLUTE")
    print arcpy.GetMessages()

I the layers are being created , but there is a problem with the definition query as what i am in getting inside the defination box for the layer is

company = ' company_query'

where is i like to see company = ' variable that is inside company_query'

  • 1
    Print your where_clause. It's almost certainly a quotes problem. I'm guessing it's interpreting it as: "COMPANY" = 'company_query' ('company_query' text, not the variable value).
    – phloem
    Jan 27, 2015 at 0:12
  • You refer to a previous "thread" but supply no link to it. Can you edit that in, please?
    – PolyGeo
    Jan 27, 2015 at 0:14
  • 1
    Also, I don't believe you can use 'Libraries\Documents' as a workspace. Use the full path, with Arc-readable slashes, like: r'C:\Users\YOUR_USER_NAME\Documents'
    – phloem
    Jan 27, 2015 at 0:22
  • You have shown some code but have not told us precisely where you are stuck i.e. what happens when you run it? If it throws an error then always include that along with the code that produced it. It may be worth reviewing meta.gis.stackexchange.com/questions/3703/… to try and keep your question from being considered too broad.
    – PolyGeo
    Jan 27, 2015 at 0:34
  • In addition to the above answers,I also noticed that you use python list as the body of the loop, so you may have to repeat the name of the company, resulting in repeated treatment. So you can convert Company_list to a python set. Company_list= [row[0] for row in arcpy.da.SearchCursor(to_be_processed,"COMPANY")] Company_list=set(Company_list)
    – T J
    Jan 27, 2015 at 0:46

1 Answer 1


Instead of using:

where_clause = '"COMPANY" = \'company_query\''

One way is to use:

where_clause = '"COMPANY" = ' + "'" + company_query + "'"

Around company_query I have double-single-double quotes and before COMPANY there is a single-double quote.

However, I prefer to use Python string formatting which I find simpler and easier to read:

where_clause = "COMPANY = '{0}’".format(company_query)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.