Search Cursor against date field and using date with python

904
4
Jump to solution
05-16-2014 12:23 PM
AliciaMein
New Contributor III
Good Afternoon ArcPy Wizards!

I would love some input on my code.
I am trying to capture the earliest positive in a grid and then count  the negative sample that were required before the positive.

The Code:

 for grid in grids:         dates = []         arcpy.SelectLayerByLocation_management("cwdpts4Anlys", "WITHIN", grid.shape, "NEW_SELECTION")         positives = arcpy.SearchCursor("cwdpts4Anlys",' "ELISALYMPHNODE" = \'Positive\' ' , "", "SAMPLE_ DATE" ')          for positive in positives:             dates.append(row[0])                  earliest = min(dates)         arcpy.SelectLayerByLocation_management("cwdpts4Anlys", "WITHIN", grid.shape, "NEW_SELECTION")         arcpy.SelectLayerByAttribute_management("cwdpts4Anlys", "SUBSET_SELECTION", ' "ELISALYMPHNODE" = \'Negative\' ')         arcpy.SelectLayerByAttribute_management("cwdpts4Anlys","SUBSET_SELECTION",'"SAMPLE_DATE" <= date earliest ' )         negatives = int(arcpy.GetCount_management("cwdpts4Anlys").getOutput(0))         grid.setValue('NegPrior2Pos', negatives)         grids.updateRow(grid)         

I get a syntax error at
positives = arcpy.SearchCursor("cwdpts4Anlys",' "ELISALYMPHNODE" = \'Positive\' ' , "", "SAMPLE_ DATE" ')


Ive tried adding the keyword date before "SAMPLE_DATE" and many other ways.

Thanks!
Alicia
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
 positives = arcpy.SearchCursor("cwdpts4Anlys",' "ELISALYMPHNODE" = \'Positive\' ' , "", "SAMPLE_ DATE" ' 


Just a quick note: you have it as "SAMPLE_ DATE", not "SAMPLE_DATE".  It could just be a copy/paste into the forum post editor, but you may want to take a quick look at your source code to verify this isn't the problem.

View solution in original post

0 Kudos
4 Replies
JamesCrandall
MVP Frequent Contributor
 positives = arcpy.SearchCursor("cwdpts4Anlys",' "ELISALYMPHNODE" = \'Positive\' ' , "", "SAMPLE_ DATE" ' 


Just a quick note: you have it as "SAMPLE_ DATE", not "SAMPLE_DATE".  It could just be a copy/paste into the forum post editor, but you may want to take a quick look at your source code to verify this isn't the problem.
0 Kudos
IanMurray
Frequent Contributor
positives = arcpy.SearchCursor("cwdpts4Anlys",' "ELISALYMPHNODE" = \'Positive\' ' , "", "SAMPLE_ DATE" ')



I think you have an extra single quote at the end.  You single quote for the expression ends at the end of the 2nd parameter, then I see no single quote for the one on the end to go with.  SHould be like this I believe.


positives = arcpy.SearchCursor("cwdpts4Anlys",' "ELISALYMPHNODE" = \'Positive\' ' , "", "SAMPLE_ DATE" )



And yes, syntax errors are the devil.
0 Kudos
AliciaMein
New Contributor III

positives = arcpy.SearchCursor("cwdpts4Anlys",' "ELISALYMPHNODE" = \'Positive\' ' , "", "SAMPLE_ DATE" '



Just a quick note: you have it as "SAMPLE_ DATE", not "SAMPLE_DATE".  It could just be a copy/paste into the forum post editor, but you may want to take a quick look at your source code to verify this isn't the problem.


I think your right, but I am also curious about how the date format is maintained.

    When I set up the "dates" list, what is the format of the list?
   
Thanks,
Alicia
0 Kudos
AliciaMein
New Contributor III
positives = arcpy.SearchCursor("cwdpts4Anlys",' "ELISALYMPHNODE" = \'Positive\' ' , "", "SAMPLE_ DATE" ')



I think you have an extra single quote at the end.  You single quote for the expression ends at the end of the 2nd parameter, then I see no single quote for the one on the end to go with.  SHould be like this I believe.


positives = arcpy.SearchCursor("cwdpts4Anlys",' "ELISALYMPHNODE" = \'Positive\' ' , "", "SAMPLE_ DATE" )



And yes, syntax errors are the devil.


I missed that! Thanks
0 Kudos