Skip to main content
deleted 37 characters in body
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Why would you want to avoid using anAn update Cursor? They will out perform the field calculator 100% of the time. You

You need to write this as an expression:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
exp = '''def add_date():
  import time
  return time.strftime("%Y/%m/%d")'''

arcpy.CalculateField_management(fc, field, 'add_date()',
                                'PYTHON', exp)
print 'done'

EDIT: datetime.date.today() did not work in field calculator, switched to strftime.

OR, if you want to do it the better way where you can feed your own variables in, use a cursor:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])
print 'done'

Why would you want to avoid using an update Cursor? They will out perform the field calculator 100% of the time. You need to write this as an expression:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
exp = '''def add_date():
  import time
  return time.strftime("%Y/%m/%d")'''

arcpy.CalculateField_management(fc, field, 'add_date()',
                                'PYTHON', exp)
print 'done'

EDIT: datetime.date.today() did not work in field calculator, switched to strftime.

OR, if you want to do it the better way where you can feed your own variables in, use a cursor:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])
print 'done'

An update Cursor will out perform the field calculator 100% of the time.

You need to write this as an expression:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
exp = '''def add_date():
  import time
  return time.strftime("%Y/%m/%d")'''

arcpy.CalculateField_management(fc, field, 'add_date()',
                                'PYTHON', exp)
print 'done'

datetime.date.today() did not work in field calculator, switched to strftime.

OR, if you want to do it the better way where you can feed your own variables in, use a cursor:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])
print 'done'
added 89 characters in body
Source Link
crmackey
  • 8.5k
  • 19
  • 33

Why would you want to avoid using an update Cursor? They will out perform the field calculator 100% of the time. You need to write this as an expression:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
exp = '''def add_date():
  import datetimetime
  return datetime.datetime.todaystrftime("%Y/%m/%d")'''

arcpy.CalculateField_management(fc, field, 'add_date()',
                                'PYTHON', exp)
print 'done'

EDIT: datetime.date.today() did not work in field calculator, switched to strftime.

OR, if you want to do it the better way where you can feed your own variables in, use a cursor:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])
print 'done'

Why would you want to avoid using an update Cursor? They will out perform the field calculator 100% of the time. You need to write this as an expression:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
exp = '''def add_date():
  import datetime
  return datetime.date.today()'''

arcpy.CalculateField_management(fc, field, 'add_date()',
                                'PYTHON', exp)
print 'done'

OR, if you want to do it the better way where you can feed your own variables in, use a cursor:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])
print 'done'

Why would you want to avoid using an update Cursor? They will out perform the field calculator 100% of the time. You need to write this as an expression:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
exp = '''def add_date():
  import time
  return time.strftime("%Y/%m/%d")'''

arcpy.CalculateField_management(fc, field, 'add_date()',
                                'PYTHON', exp)
print 'done'

EDIT: datetime.date.today() did not work in field calculator, switched to strftime.

OR, if you want to do it the better way where you can feed your own variables in, use a cursor:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])
print 'done'
Source Link
crmackey
  • 8.5k
  • 19
  • 33

Why would you want to avoid using an update Cursor? They will out perform the field calculator 100% of the time. You need to write this as an expression:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
exp = '''def add_date():
  import datetime
  return datetime.date.today()'''

arcpy.CalculateField_management(fc, field, 'add_date()',
                                'PYTHON', exp)
print 'done'

OR, if you want to do it the better way where you can feed your own variables in, use a cursor:

import arcpy, datetime

fc = r'C:\GIS\CARGIS\SHAPES.gdb\CRASH_ON_2013'
field = "DTCARXTRCT"
with arcpy.da.UpdateCursor(fc, [field]) as rows:
    for row in rows:
        rows.updateRow([datetime.date.today()])
print 'done'