Problem with select analysis + copy rows in loop

6508
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
SepheFox
Frequent Contributor

I don't really understand your select query. Why are you including the " + ? Don't you just want it to be the list? Perhaps I'm missing something.

0 Kudos
SepheFox
Frequent Contributor

Any how, try doing something like this: " \"FID\" = ' + list + ' "

To quote the ArcGIS help "In Python, strings are enclosed in matching single or double quotes. To create a string that contains quotes (as is common with a WHERE clause in SQL expressions), you can escape the quotes (using a backslash) or triple quote the string."

SepheFox
Frequent Contributor

You know, just reading through this thread again, and I see you are using a point shapefile. Make feature layer only works on feature classes. And, yes, your workspace needs to be a GDB or a folder, not an MXD.

KONPETROV
Occasional Contributor III

Sephe thanks for your reply. I tranferred everything to a folder so i don't have that problem now. With  this: " \"FID\" = ' + list + ' " i have the same error, so propably that crashes the procedure i just cannot find a way to fix it.

0 Kudos
SepheFox
Frequent Contributor

Well ​Make Feature Layer can only work with a feature class. Can you make a gdb and export your shapefile into it?

0 Kudos
KONPETROV
Occasional Contributor III

there is no reason for that Sephe, i don't need an gdb i can just play with my files, deleting my .mxd.

0 Kudos
KONPETROV
Occasional Contributor III
import arcpy
from arcpy import env
env.overwriteOutput = 1
env.workspace = "c:\Task\Data\"
point = "SortB"
list = []
with arcpy.da.SearchCursor("SortB", ["OID@", "Condition"]) as cursor:
    for row in cursor:
        if row[1] == 'TRUE':
            list.append(str(row[0]))
        else:
               break
del cursor
print list
print " \"FID\" IN (" + ",".join(list) + ")"
#create a feature layer of only features within list
arcpy.MakeFeatureLayer_management("SortB", "pointLyr", " \"FID\" IN (" + ",".join(list) + ")")
#create feature class from feature layer
arcpy.CopyFeatures_management("pointLyr", "c:\Task\Data\SortBb.shp")

Dear friends this is the solution.

Thank you all for your time and your help!

JoshuaBixby
MVP Esteemed Contributor

You got so much help on this, and a couple other questions, it begs the question whether it is appropriate to mark your own answer correct.  The "Correct" answer should be the one from another user that helped the most in rewriting your code block above, at least in my opinion.

SepheFox
Frequent Contributor

Yeah, sometimes it is a grey area, and you come up with the answer yourself, although people may try to help, but I think I would have said Jake Skinner wrote most of the final piece of code in this particular post.

BlakeTerhune
MVP Regular Contributor

I think you can mark your own reply as correct if it includes the full solution from various sources and the correct attribution is given to the original authors. Anyone else who stumbles upon this thread with a similar issue would want quick access to the final solution, not just which one helped the most in getting there. Ideally the reply marked as correct would have all of these things.