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:
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.
Solved! Go to Solution.
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?
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")
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.
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 + ")")
Jake no i don't have an Event Layer. i just have a shapefile with many pnts and fields.
The same will apply to a shapefile. The shapefile will not have a field called OBJECTID.
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")
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).
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.
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)