Problem with select analysis + copy rows in loop

4675
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
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Kon,

Here is an example on how you can do this:

import arcpy

from arcpy import env

env.overwriteOutput = 1

env.workspace = r"C:\temp\python\test.gdb"

point = "Point"

list = []

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

    for row in cursor:

        if row[1] == 'True':

            x = row[0]

        elif row[1] == 'False':

            list.append(x)

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")

View solution in original post

31 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Why don't you do the process manually once, then copy and modify the python code snippet from the Results Window (under Customize menu).  I have documented several examples of using this approach to develop scripts that employ Arctoolbox tools.  Also, I am not sure which field you are querying nor what you are updating given you are using an update cursor.

KONPETROV
Occasional Contributor III

Well I am not trying to update anything I am using update cursor trying to iterate from row to row. I cannot think another way. In Python I know how to do it but the combinations with ArcPy cause me a big headache. Can you paste a link to your approach examples?

BlakeTerhune
MVP Regular Contributor

There is a lot of little things wrong with your code. Before I try creating new code, I have a question. It looks like you have a second condition you are using to filter your table, could you please describe that? Right now I see the only thing you check is that the Condition field is equal to "TRUE". What criteria did you use to eliminate FID 4 and 5?

KONPETROV
Occasional Contributor III

I want the procedure to stop when it finds a FALSE statement but i don't know how to write it, this is the criteria for the elimination of all values after the first false. I just want to do a check with while (while Condition = True) and copy only them until TRUE become FALSE.

0 Kudos
BlakeTerhune
MVP Regular Contributor

That's not reliable because you cannot guarantee the order in which the rows are returned. Could you describe in more detail why you only want to copy rows until it finds a FALSE?

KONPETROV
Occasional Contributor III

These are distances betweens points along a route. When i have a false statement that means that i must stop there and copy all the records until that point. That is why i want all the records until the first FALSE Statement.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Kon,

Here is an example on how you can do this:

import arcpy

from arcpy import env

env.overwriteOutput = 1

env.workspace = r"C:\temp\python\test.gdb"

point = "Point"

list = []

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

    for row in cursor:

        if row[1] == 'True':

            x = row[0]

        elif row[1] == 'False':

            list.append(x)

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")

View solution in original post

BlakeTerhune
MVP Regular Contributor

for row in cursor:

    if row[1] == 'True':

        x = row[0]

    elif row[1] == 'False':

        list.append(x)

Wouldn't that only get the last row that is TRUE? x gets replaced each time it's true and only gets appended to list when it finds a FALSE. Seems like you'd have to do something like

for row in cursor:

    if row[1] == 'True':

        list.append(row[0])

    elif row[1] == 'False':

        break

KONPETROV
Occasional Contributor III

Jake thanks for your response but i tested it with the correction of Mr. Blake T and i got an "ERROR 000230: Failed selecting with OBJECTID in () Failed to execute (MakeFeatureLayer).  "

0 Kudos