Problem with select analysis + copy rows in loop

6520
31
Jump to solution
04-03-2015 07:49 AM
KONPETROV
Occasional Contributor III

Hi, i am trying to implement various combinations of tools trying to export some rows  from a pnt shapefile based on a query (or while or for loop).I have a column (Condition) with values TRUE and FALSE and i want to export every row for the statement,  Condition = "TRUE". However i only want those rows who respond to TRUE and if a FALSE follows i don't want to export nothing else, although after FALSE maybe there are more TRUE.

an example:

FID  Condition    H    Wo                                                  FID  Condition                                        H         Wo
0      TRUE         65    20                                                     0      TRUE                                              65        20
1      TRUE         54    25                                                     1      TRUE                                              54        25
2      TRUE         54     0      I want only these values  --->   2      TRUE   with the rest of the fields    54         0
3      FALSE       65    25                                                                             
4      TRUE         98     0
5      TRUE         23     5

These are the samples of code i tried, to export the rows based my statement :

import arcpy, os

fc = "c:/Task/data/In.shp"

outworkspace = "c:/Task/data/In.shp"

fields = ["FID", "Condition", "H", "Wo"]

with arcpy.da.UpdateCursor(fc, fields) as cursor:

     for row in cursor:

         while [row] = 'TRUE':

         arcpy.CopyRows_management("In", "c:/Task/data/Out.shp")

and also with select analysis:

import arcpy, os

from arcpy import env

env.workspace = "c:/Task/data/In.shp"

fc = "c:/Task/data/In.shp"

fields = ["FID", "Condition", "H", "Wo"]

with arcpy.da.UpdateCursor(fc, fields) as cursor:

     for row in cursor:

         while [row] = 'TRUE':

         arcpy.Select_analysis("In", "Out_s", '[Condition] = "TRUE"'))

But for some reason i always get an error.

0 Kudos
31 Replies
JakeSkinner
Esri Esteemed Contributor

I misunderstood  your initial question.  I thought you only wanted the first True condition before the False condition.  If you want all True conditions, Blake's update should work. 

It looks like the code is failing when you are trying to create the Feature Layer.  Can you post the code you tried?

KONPETROV
Occasional Contributor III

right away:

import arcpy

... from arcpy import env

... env.overwriteOutput = 1

... env.workspace = "c:\Task\Data\In.mxd"

... point = "route"

... list = []

... with arcpy.da.SearchCursor(point, ["OID@", "Condition"]) as cursor:

...     for row in cursor:

...         if row[1] == 'True':

...             list.append(row[0])

...         elif row[1] == 'False':

...             break  

... 

... del cursor

... 

... #convert list to string

... list = str(list).strip('[]')

... 

... #create a feature layer of only features within list

... arcpy.MakeFeatureLayer_management(point, "pointLyr", "OBJECTID in (" + list + ")")

... 

... #create feature class from feature layer

... arcpy.CopyFeatures_management("pointLyr", "pointSelection")

0 Kudos
BlakeTerhune
MVP Regular Contributor

I think your workspace has to be a geodatabase, not an MXD. Also, the point variable is the name your input feature class (not a the layer name from an MXD).

EDIT

You may also need to change the strings in the if statement from "True" to "TRUE" and "False" to "FALSE" so it matches your data.

JakeSkinner
Esri Esteemed Contributor

Are you working with a route Event Layer?  The event layer most likely does not have a field called OBJECTID.  Open the attribute table and see what this field may be.  For example, it may be FID.

Once you have this field, update the arcpy.MakeFeatureLayer_management command.  Ex:

arcpy.MakeFeatureLayer_management(point, "pointLyr", "FID in (" + list + ")")

KONPETROV
Occasional Contributor III

Jake no i don't have an Event Layer. i just have a shapefile with many pnts and fields.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

The same will apply to a shapefile.  The shapefile will not have a field called OBJECTID.

JakeSkinner
Esri Esteemed Contributor

Also, you will need to update the last line to write the output to a shapefile or geodatabase.  Ex:

arcpy.CopyFeatures_management("pointLyr", r"C:\temp\pointSelection.shp")

0 Kudos
KONPETROV
Occasional Contributor III

Jade thanks a lot for your reply, but when i test it i get back an 00230 error: ERROR 000230: Failed selecting with FID in () Failed to execute (MakeFeatureLayer). 

000230 : Failed selecting with <value>.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Did you check the help file on that error message?  In short, it means that the query doesn't work.  Did you try it manually?  Did you verify that it worked? Did you read the help topic on about building an SQL expression?  When you can perform the tasks manually, documenting what you did and what worked, you will have the workings of a script.  I will emphasize that using the Results window (see the Customize menu) and using tools from ArcToolbox is the fastest way of building a script that works from start to finish.

KONPETROV
Occasional Contributor III

Mr Patterson i read it, and i saw the query again. I guess something is going wrong

with FID in (" + list + ")"   

but i have nerer use an expression like this to correct it. In my mind for my task was only select analysis in a while loop, this is a different approach and propably the correct but something is wrong in the code.

Manually i did it with my code and it worked, but the problem i have here is that i cannot do it for an example like this I shew you in my example, where TRUE and FALSE statements are mixed. 

You mentioned that with results window again, i always see what the results window appear and here isn't not showing something different than the error in Python window. It just says :

Invalid SQL statement was used  and

Failed to execute (Make Feature Layer)

0 Kudos