i have some code that takes a field and finds every unique value in that field. it then creates a layer for each unique value.

The code is working ok , but it is failing when there are \ characters within the value , for example "london\bridge".

If i were to do a manual defination query and save a layer it would work . So why does it not work here ? Anyone have any ideas My code uses the search curosor to get the unique values and iterates through this one at a time saving out a layer .

import arcpy
from arcpy import env
## This is where the file that is to be processed is saved  
arcpy.env.workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"
# The file to be processed 
Source_File = "SDE_COMPANY_LIST.shp"
# The attribute that is being looked at in this case is "COMPANY" , the company bulk list accesses all the values within the given feild in this case it is company 
Company_Bulk_Company_List = [row[0] for row in arcpy.da.SearchCursor(Source_File,"COMPANY")]
#the UniqueCompany variable takes all the values from the field and returns a unique set , which gives the basis for every layer to be write 
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"
# The attribute that is being looked at is repeated here 
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()

In Python a backslash is an escape character. In order to put a backslash in the string you need to need to put another backslash next to it to stop it from forcing a new line.

myString = "this is\\ a string with a backslash"

I can see your paths are set correctly but maybe the strings you are parsing only have a single backslash.

You can solve this by using a raw string.

myRawString = r"this is a string \ with a backslash"

Also, if you are trying to create a file with a backslash in the file name, it probably wont work.

A good way to debug this problem is to put in some print statements.

  • Ok so at the moment the string I am passing into my where clause is a stored as a variable called company query. " * where_clause = '"' + User_Feild + '"' + " = " + "'" + company_query + "'" * How do I convert the variable into a Rawsting something like Myrawstring = '\r'+ company_query – Jack Walker Apr 28 '15 at 8:53

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.