5

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.

2 Answers 2

5

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.

2
  • 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, 2012 at 3:53
  • +1 and accept because I like the format of your where clause.
    – Fezter
    Nov 5, 2012 at 3:54
2

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 + "'"
1
  • you can also use AddFieldDelimiters Nov 1, 2012 at 13:27

Your Answer

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

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