<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Datetime field in da.SearchCursor to csv file in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040975#M60576</link>
    <description>&lt;P&gt;Another way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fc = r'path/to/featureclass'
dateformat = '%Y-%m-%d'
datefields = [d.name for d in arcpy.ListFields(fc, field_type='Date')]
recs = []
with arcpy.da.SearchCursor(fc, ['OID@', 'TheDate']) as rows:
    for row in rows:
        recs += [r if f not in datefields else r.strftime(dateformat) for r, f in zip(row, rows.fields)]
print(recs)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or as a iterator:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def datecursor(fc, dateformat, *args, **kwargs):
    datefields = [d.name for d in arcpy.ListFields(fc, field_type='Date')]
    with arcpy.da.SearchCursor(fc, *args, **kwargs) as rows:
        for row in rows:
            yield [r if f not in datefields else r.strftime(dateformat) for r, f in zip(row, rows.fields)]

fc = r'path/to/featureclass'
dateformat = '%Y-%m-%d'
recs = [row for row in datecursor(fc, dateformat, ['OID@', 'TheDate'])]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 26 Mar 2021 11:35:58 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2021-03-26T11:35:58Z</dc:date>
    <item>
      <title>Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040744#M60559</link>
      <description>&lt;P&gt;I need to write the output of a search cursor to a csv file.&amp;nbsp; I stole an idea from &lt;A href="https://gis.stackexchange.com/questions/278351/writing-arcpy-cursor-results-to-text-file" target="_self"&gt;this Stackexchange post&lt;/A&gt; that uses list comprehension and it works great.&amp;nbsp; The problem is one of my fields is a date field, and it's goofing up the works.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
fc = r'\Path\to\my\featureclass'
fields = ['UNIQUE_ID','asset_id','Condition','Year_Installed','Carto_ID',
          'Pave_Area','Pave_Width','Length','AM_Asset_ID','Lanes','OCI_Date']
lines = [row for row in arcpy.da.SearchCursor(fc,fields)]&lt;/LI-CODE&gt;&lt;P&gt;Here is what one of the tuples in the lines list looks like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt; ('10100922','PWOPS_pave00024318',84,1989,7366264,8800.60992698,35.0,
  251.44599791,None,None,  datetime.datetime(2019, 2, 1, 0, 0))&lt;/LI-CODE&gt;&lt;P&gt;Notice how the field OCI_date comes from the search cursor. I get what it's doing with the datetime.datetime() method, but how can I avoid that?&amp;nbsp; I guess I could just append every row to the list as individual tuple elements and for the date, format in a fashion that works, but maybe there is a better way?&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 18:51:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040744#M60559</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-03-25T18:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040749#M60561</link>
      <description>&lt;P&gt;Try using&amp;nbsp;&lt;A href="https://www.programiz.com/python-programming/datetime/strftime" target="_self"&gt;datetime&amp;nbsp;strftime&lt;/A&gt; to format the date as a string in whatever style you want. You should probably do this in a traditional for loop rather than list comprehension.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 18:56:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040749#M60561</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-03-25T18:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040751#M60562</link>
      <description>&lt;P&gt;That's what I meant by&amp;nbsp;&lt;EM&gt;I guess I could just append every row to the list as individual tuple elements.&amp;nbsp;&amp;nbsp;&lt;/EM&gt;I was hoping to keep in the list comprehension format, but I suspect opening an empty list and appending a formatted tuple to it is the only option...&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 18:58:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040751#M60562</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-03-25T18:58:14Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040754#M60564</link>
      <description>&lt;P&gt;You could keep the list comprehension to create &lt;FONT face="courier new,courier"&gt;lines&lt;/FONT&gt; and then go back over &lt;FONT face="courier new,courier"&gt;lines&lt;/FONT&gt; with a for loop to format the datetime objects. Or write a helper function to check the value for a date and return the formatted date string during the list comprehension.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 19:02:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040754#M60564</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-03-25T19:02:46Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040770#M60565</link>
      <description>&lt;LI-CODE lang="python"&gt;import arcpy
from datetime import datetime

fc = r'\Path\to\my\featureclass'
fields = ['UNIQUE_ID','asset_id','Condition','Year_Installed','Carto_ID',
          'Pave_Area','Pave_Width','Length','AM_Asset_ID','Lanes','OCI_Date']
lines = [dtFormat(row) for row in arcpy.da.SearchCursor(fc,fields)]

def dtFormat(row):
    try:
        OCI_Date_index = row[row.index('OCI_Date')]
        row[OCI_Date_index] = row[OCI_Date_index].strftime('%m/%d/%Y')
        return row
    except AttributeError:
        return row&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 25 Mar 2021 19:15:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040770#M60565</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-03-25T19:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040772#M60566</link>
      <description>&lt;P&gt;Sweet.&amp;nbsp; Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 19:16:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040772#M60566</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-03-25T19:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040773#M60567</link>
      <description>&lt;P&gt;Completely untested, but I think you get the idea.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 19:17:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040773#M60567</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-03-25T19:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040797#M60570</link>
      <description>&lt;P&gt;A wild guess, if you would consider tab delimited:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;lines = ["\t".join(str(i) for i in row) for row in arcpy.da.SearchCursor("MyFeature",["*"])]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You could join with a comma instead of the tab.&amp;nbsp; Just a quick test in desktop, so it is 2.7 python.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 19:39:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040797#M60570</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2021-03-25T19:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040806#M60571</link>
      <description>&lt;P&gt;You could go with a nested list comprehension:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;lines = [
    [col.strftime('%m/%d/%Y') if isinstance(col, datetime.datetime) else col for col in row]
    for row
    in arcpy.da.SearchCursor(fc, fields)
]&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 25 Mar 2021 19:53:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040806#M60571</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-03-25T19:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: Datetime field in da.SearchCursor to csv file</title>
      <link>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040975#M60576</link>
      <description>&lt;P&gt;Another way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fc = r'path/to/featureclass'
dateformat = '%Y-%m-%d'
datefields = [d.name for d in arcpy.ListFields(fc, field_type='Date')]
recs = []
with arcpy.da.SearchCursor(fc, ['OID@', 'TheDate']) as rows:
    for row in rows:
        recs += [r if f not in datefields else r.strftime(dateformat) for r, f in zip(row, rows.fields)]
print(recs)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or as a iterator:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def datecursor(fc, dateformat, *args, **kwargs):
    datefields = [d.name for d in arcpy.ListFields(fc, field_type='Date')]
    with arcpy.da.SearchCursor(fc, *args, **kwargs) as rows:
        for row in rows:
            yield [r if f not in datefields else r.strftime(dateformat) for r, f in zip(row, rows.fields)]

fc = r'path/to/featureclass'
dateformat = '%Y-%m-%d'
recs = [row for row in datecursor(fc, dateformat, ['OID@', 'TheDate'])]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 11:35:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/datetime-field-in-da-searchcursor-to-csv-file/m-p/1040975#M60576</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-03-26T11:35:58Z</dc:date>
    </item>
  </channel>
</rss>

