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 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.
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."
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.
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.
Well Make Feature Layer can only work with a feature class. Can you make a gdb and export your shapefile into it?
there is no reason for that Sephe, i don't need an gdb i can just play with my files, deleting my .mxd.
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!
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.
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.
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.