Hi all. I am trying to write a script in Python that will populate a date field in a featureclass with a date of my choosing. Not today's date. Can anybody point me in the right direction?
Thanks!
First you need to create an expression value for the field calculation:
exp <SPAN class="operator token">=</SPAN> <SPAN class="string token">'datetime.datetime(2019,1,17).strftime("%Y/%m/%d")'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
Then do the field calculation:
arcpy<SPAN class="punctuation token">.</SPAN>CalculateField_management<SPAN class="punctuation token">(</SPAN>A_OP_LISTBUILDINGS<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"DateFrom"</SPAN><SPAN class="punctuation token">,</SPAN>exp<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"PYTHON_9.3"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
<SPAN class="" style="color: #669900; border: 0px; font-weight: inherit; font-size: 14px;">.strftime("%Y/%m/%d")</SPAN>
This was what i was missing!! Thanks so much! You legend
Hi Randy! Thanks very much for your answer!
I am using PyCharm 2018.3.3 and ArcGIS Pro 10.5 with a File GDB
My current attempt is:
import arcpyimport datetime
arcpy.env.workspace = r"xyz.gdb"
arcpy.env.overwriteOutput = True
datefrom = datetime.datetime(2019,01,17)dateto = datetime.datetime(2020,01,01)
arcpy.MakeFeatureLayer_management("L_LISTED_BUILDINGS", "LISTED_BUILDINGS")
A_OP_LISTBUILDINGS = arcpy.SelectLayerByLocation_management('LISTED_BUILDINGS', 'INTERSECT', 'FEATURE', 0, 'NEW_SELECTION')
A_OP_LISTBUILDINGS = arcpy.CalculateField_management(A_OP_LISTBUILDINGS,"DateFrom",datefrom)A_OP_LISTBUILDINGS = arcpy.CalculateField_management(A_OP_LISTBUILDINGS,"DateTo",dateto)
arcpy.CopyFeatures_management(A_OP_LISTBUILDINGS, 'A_OP_190117_LISTBUILDINGS')
However this populates the field with "00:00:00" i.e assuming it's a time. I think this is because it is in the format yyyy,mm,dd but that is the only format datetime.datetime accepts (I think).
Any ideas?
Many thanks
Rob
You should be familiar with Python's datetime module and ArcGIS' Update Cursor tool. For a more specific answer, you should describe your work environment. What version of Python and ArcGIS are you using? What is the type of geodatabase?
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.