I would like to write a script where I select a feature class by attributes, add a field for a timestamp, and append the selected features to another feature class. My code is below; Do I need to create a feature layer before selecting by attribute and is it possible to select the layer by attribute, and append the selection another feature class? My code is below;
import arcpy
SDE5556 = "C:\CleanStreets_55_56.gdb\Clean_Streets_55_56"
SOSC = "C:\CleanStreets_55_56.gdb\SOSC"
AppendPath = "C:\CleanStreets_55_56.gdb"
AppendTable = "CleanTable"
aTable = "C:\CleanStreets_55_56.gdb\CleanTable"
Clause= "' PlannedDate = CONVERT(DATE, GETDATE()) AND RESOLUTION_CODE = '55'"
arcpy.MakeFeatureLayer_management (SOSC, "sosc")
arcpy.SelectLayerByAttribute_management(SOSC, "NEW_SELECTION ", Clause)
arcpy.AddField_management(PeterTable, "DATE", "DATE")
dateExpression = "Date"
arcpy.CalculateField_management(aTable, "DATE", dateExpression)
arcpy.Append_management(SOSC, SDE5556, NO_TEST)
When executing the first two lines of the main code block in the Python window in ArcMap I receive Runtime error Traceback (most recent call last): File "<string>", line 14, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6688, in SelectLayerByAttribute raise e ExecuteError: ERROR 000800: The value is not a member of NEW_SELECTION | ADD_TO_SELECTION | REMOVE_FROM_SELECTION | SUBSET_SELECTION | SWITCH_SELECTION | CLEAR_SELECTION.
-
2there's a leading ' in your clause "' PlannedDate = becomes "PlannedDate =. Is RESOLUTION_CODE a string field? if it's numeric it's AND RESOLUTION_CODE = 55 also it's "NEW_SELECTION" not "NEW_SELECTION " the trailing space needs to be removed. You know you can do arcpy.MakeFeatureLayer_management (SOSC, "sosc", Clause) and skip the SelectLayerByAttribute..Michael Stimson– Michael Stimson2015年04月16日 23:07:33 +00:00Commented Apr 16, 2015 at 23:07
-
It is a string.Geoffrey West– Geoffrey West2015年04月16日 23:08:36 +00:00Commented Apr 16, 2015 at 23:08
2 Answers 2
You have an extra space in your code. And you need to apply the selection to the feature layer you created.
Change:
arcpy.SelectLayerByAttribute_management(SOSC, "NEW_SELECTION ", Clause)
to:
arcpy.SelectLayerByAttribute_management("sosc", "NEW_SELECTION", Clause)
I also foresee problems with you field calculator. And the variable PeterTable
isn't defined. And you probably want to use your "sosc"
layer as the input for your append I'm guessing.
The easiest way to do this without demystifying feature layers was to create another feature class and use the where clause there then proceed with the magic of search cursors etc.
SCDE5556= "feature1"
SOSC = "feature2"
Clause1= "PlannedDate = CONVERT(DATE, GETDATE()) AND RESOLUTION_CODE IN ( '55', '56')"
Clause2= "NUMBERCYLA > 'SR07885524' AND RESOLUTION_CODE IN ( '55', '56')"
Copy = "CopyFeature"
try:
if arcpy.Exists(Copy):
arcpy.Delete_management(Copy)
arcpy.FeatureClassToFeatureClass_conversion(SOSC, "aPath", "aFC", Clause1)
arcpy.AddField_management(Copy, "DATE", "DATE")
dateExpression = "Date"
arcpy.CalculateField_management(Copy, "DATE", dateExpression)
cursorClause = "DATE = current_date"
cursor = arcpy.UpdateCursor(Copy, Clause2)
for row in cursor:
row.setValue("Prior_RESOLUTION_CODE", row.getValue("RESOLUTION_CODE"))
cursor.updateRow(row)
fname = "Zero"
Zero = 0
arcpy.AddField_management(Copy, "Zero", "TEXT")
arcpy.CalculateField_management(Copy, fname, Zero)
cursor = arcpy.UpdateCursor(Copy, Clause2)
for row in cursor:
row.setValue("RESOLUTION_CODE", row.getValue("Zero"))
cursor.updateRow(row)
arcpy.Append_management(Copy, SDE5556, "NO_TEST")
print "success"
except:
logger.exception("Something has gone wrong!")