Hi all,
I am trying to 1) Select by Attribute and 2) overwrite a feature class with selected features. Below is the script that I'm trying as a start.. and am getting an error related to Select by Attribute. Ultimately, need to select last 365 days of data (not Baker neighborhood) and overwrite a feature class.. on a daily basis. Thank you in advance for any assistance! Shelby
# Description: Extract features to a new feature class based on an attribute query
# Import system modules
import arcpy
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management ("D:/pd/crimeD.gdb/dencrime", "crimelayer")
# Select all crimes that happened in Baker
arcpy.SelectLayerByAttribute_management ("crimelayer", "NEW_SELECTION", ' "NEIGHBORHOOD_ID" = baker ')
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("crimelayer", "Bakercrime")
Solved! Go to Solution.
It is likely the query you are using. the value Baker should be between quotes and the field name (if FC is stored in fgdb) would be without quotes. Something like this:
arcpy.SelectLayerByAttribute_management ("crimelayer", "NEW_SELECTION", "NEIGHBORHOOD_ID = 'baker'")
Sorry no access to my computer at this time to verify a little more.
With the shell script what is the error just in case
Secondly...
More questions pending the answers to the above.
Thank you Dan!
This is the error that I'm getting:
Traceback (most recent call last):
File "<string>", line 7, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 6569, in SelectLayerByAttribute
raise e
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 6566, in SelectLayerByAttribute
retval = convertArcObjectToPythonObject(gp.SelectLayerByAttribute_management(*gp_fixargs((in_layer_or_view, selection_type, where_clause, invert_where_clause), True)))
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).
Regarding your other questions, the date is in a field and is "date" data type. Yes, they are sequential dates from 2010 through today. Hundreds per day.
Much appreciated, Shelby
It is likely the query you are using. the value Baker should be between quotes and the field name (if FC is stored in fgdb) would be without quotes. Something like this:
arcpy.SelectLayerByAttribute_management ("crimelayer", "NEW_SELECTION", "NEIGHBORHOOD_ID = 'baker'")
Sorry no access to my computer at this time to verify a little more.
Thank you Xander, this worked!
Hi Dan_Patterson, I wonder if you know how to query for data from the last 365 days using a date field? Thank you! -Shelby
import datetime
n = datetime.datetime.now() # ---- now
t = datetime.timedelta(days=365) # ---- used timedelta options
print("Now {} \nThen {}".format(n, n-t))
Now 2017-10-24 15:05:40.710201
Then 2016-10-24 15:05:40.710201
# ---- see the help for working with arcpy time objects
Shelby.... To give you some ideas
Thank you for this, will be testing this! Was able to make it work with the help of a colleague:
arcpy.SelectLayerByAttribute_management("crimelayer", "NEW_SELECTION", "REPORTED_DATE > timestamp '{0}'".format(str(year_ago)), None)