2

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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 16, 2015 at 22:59
2
  • 2
    there'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.. Commented Apr 16, 2015 at 23:07
  • It is a string. Commented Apr 16, 2015 at 23:08

2 Answers 2

1

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.

answered Mar 16, 2016 at 20:58
0

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!")
Midavalo
30k11 gold badges53 silver badges108 bronze badges
answered Apr 17, 2015 at 5:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.