Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am writing a python script for ArcGIS. I have an input feature class which contains multiple pipelines which is saved as a variable InputPipeline. I would like to iterate through the pipelines and run some run some processes on them individually.

The script is set up to prompt the user to choose the field which contains a unique identifier for each pipeline. I am saving this field as a variable called PipelineIDField.

I run a search cursor to get the actual pipe name in that field and I'm saving that as fullRouteName.

I'm using:

arcpy.MakeFeatureLayer_management(InputPipeline, 'SelectedPipe', where_clause)

and the where clause is defined as:

where_clause=""""%s" =""" %PipelineIDField + """ '%s' """ %fullRouteName

I would assume that I could then successfully use 'SelectedPipe' to run my processes on the individual pipe.

This is successfully working when I run the where clause in the python window within ArcGIS, however, it doesn't select out the individual pipes in my script. The where clauses are identical.

I have tried using escape characters "\"" and "\'" with the same result.

Any idea what is going on here?

I'm using ArcGIS 10.1 service pack 1 on Windows 7 64-bit.

Thanks.

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

Please see this answer for a function you can use for this purpose: How do I include a variable in the where clause of arcpy.Select_analysis?

Also you are doing something rather odd with your second line:

where_clause=""""%s" =""" %PipelineIDField + """ '%s' """ %fullRouteName

If I were to re-write this (not using the linked function) I would do something like:

where_clause = "\"%s\" = '%s'" % (PipelineIDField, fullRouteName)

As for the rest of your question I think you will need to share the rest of your script.

share|improve this answer
    
I actually had your suggested where_clause at first almost verbatim. It still wasn't working. Turns out the problem wasn't the where clause. I had: arcpy.MakeFeatureLayer_management and then was trying to reproject that layer. However, I needed to use arcpy.CopyFeatureLayer_management to write the feature class to my geodatabase because you cannot project a feature class in memory. –  Fezter Nov 5 '12 at 3:53
    
+1 and accept because I like the format of your where clause. –  Fezter Nov 5 '12 at 3:54
add comment

I prefer to build my clauses out without variable substitution -- although blah's suggestion looks great. I guess it's sorta the JavaScript way of doing things, but there are no special or escape characters needed.

whereClause = '"' + PipelineIDField + '" = ' + "'" + fullRouteName + "'"
share|improve this answer
    
you can also use AddFieldDelimiters –  mhoran_psprep Nov 1 '12 at 13:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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