<?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 skip certain fields in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286200#M22133</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am able to run a script that will calculate the time spent onsite using the datetime field for our service in and out times.&amp;nbsp; The issue is the script stops (error) when it encounters a datetime field with no time in it (working on making sure that doesn't happen).&amp;nbsp; I am wondering if there is a quick python addition I can add that when it encounters a datetime field with no time, it will skip over that record and keep running through the records.&amp;nbsp; The records are a large amount and this is getting time consuming to keep starting the script at the next record after it error'd out on a record with no time in the field.&amp;nbsp; I've attached a screenshot of an example.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 21 Apr 2016 16:23:18 GMT</pubDate>
    <dc:creator>ChadCunningham</dc:creator>
    <dc:date>2016-04-21T16:23:18Z</dc:date>
    <item>
      <title>skip certain fields</title>
      <link>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286200#M22133</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am able to run a script that will calculate the time spent onsite using the datetime field for our service in and out times.&amp;nbsp; The issue is the script stops (error) when it encounters a datetime field with no time in it (working on making sure that doesn't happen).&amp;nbsp; I am wondering if there is a quick python addition I can add that when it encounters a datetime field with no time, it will skip over that record and keep running through the records.&amp;nbsp; The records are a large amount and this is getting time consuming to keep starting the script at the next record after it error'd out on a record with no time in the field.&amp;nbsp; I've attached a screenshot of an example.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 Apr 2016 16:23:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286200#M22133</guid>
      <dc:creator>ChadCunningham</dc:creator>
      <dc:date>2016-04-21T16:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: skip certain fields</title>
      <link>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286201#M22134</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;datetime objects are the worst.&amp;nbsp; I usually convert them to strings&lt;/P&gt;&lt;P&gt;This is an obviously awful way to check whether there is a time, in the absence of a... 'has_time'... property&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; d
datetime.datetime(2016, 4, 21, 13, 16, 22, 961326)
&amp;gt;&amp;gt;&amp;gt; ds = str(d)
&amp;gt;&amp;gt;&amp;gt; ds
'2016-04-21 13:16:22.961326'
&amp;gt;&amp;gt;&amp;gt; if " " in ds: print("no time")
no time&lt;/PRE&gt;&lt;P&gt;I hope you get the drift, and I only recommend it in the absence a correct answer&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if " " in str(yourdate): skip&lt;/P&gt;&lt;P&gt;else:&amp;nbsp; do stuff&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:50:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286201#M22134</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T13:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: skip certain fields</title>
      <link>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286202#M22135</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What about:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def fnElapsed(STRDTTM, ENDDTTM):
&amp;nbsp;&amp;nbsp;&amp;nbsp; from datetime import datetime
&amp;nbsp;&amp;nbsp;&amp;nbsp; if all((STRDTTM, ENDDTTM))):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dateStart = datetime.strptime(STRDTTM, "%m/%d/%Y %I:%M:%S %p")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dateEnd = datetime.strptime(ENDDTTM, "%m/%d/%Y %I:%M:%S %p")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; timeDiff = dateEnd - dateStart
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elapMin = timeDiff.total_seconds()/60
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return elapMin&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;UPDATE:&amp;nbsp; To elaborate a little....&amp;nbsp; Similar to other data types, when a cursor encounters a Date field with no value, i.e., a NULL, it returns a Python &lt;SPAN style="font-family: courier new,courier;"&gt;NoneType&lt;/SPAN&gt; as opposed to a Python &lt;SPAN style="font-family: courier new,courier;"&gt;datetime.datetime&lt;/SPAN&gt;.&amp;nbsp; The Python &lt;A href="https://docs.python.org/2/library/functions.html#all" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;all()&lt;/SPAN&gt; &lt;/A&gt;built-in will return &lt;SPAN style="font-family: courier new,courier;"&gt;False&lt;/SPAN&gt; if one of the fields returns a &lt;SPAN style="font-family: courier new,courier;"&gt;NoneType&lt;/SPAN&gt;, effectively skipping those records without a start and end date.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:50:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286202#M22135</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T13:50:11Z</dc:date>
    </item>
    <item>
      <title>Re: skip certain fields</title>
      <link>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286203#M22136</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can check if the first format throws an error, and fix it if so. Also, you need to &lt;STRONG&gt;return&lt;/STRONG&gt; the value in the end.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;from datetime import datetime
def fnElapsed(STRDTTM, ENDDTTM):
&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateStart = datetime.strptime(STRDTTM, "%d/%m/%Y %I:%M:%S %p")
&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateStart = datetime.strptime(STRDTTM, "%d/%m/%Y")
&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateEnd = datetime.strptime(ENDDTTM, "%d/%m/%Y %I:%M:%S %p")
&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateEnd = datetime.strptime(ENDDTTM, "%d/%m/%Y")
&amp;nbsp; timeDiff = dateEnd - dateStart
&amp;nbsp; return timeDiff.total_seconds()/60&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;note: my date formats are slightly different than yours.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:50:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286203#M22136</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T13:50:14Z</dc:date>
    </item>
    <item>
      <title>Re: skip certain fields</title>
      <link>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286204#M22137</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Similar to above, you can return a dummy value instead of changing the datetime format to flag your records.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;from datetime import datetime
def fnElapsed(STRDTTM, ENDDTTM):
&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateStart = datetime.strptime(STRDTTM, "%d/%m/%Y %I:%M:%S %p")
&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 9999
&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateEnd = datetime.strptime(ENDDTTM, "%d/%m/%Y %I:%M:%S %p")
&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return -9999
&amp;nbsp; timeDiff = dateEnd - dateStart
&amp;nbsp; return timeDiff.total_seconds()/60&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:50:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286204#M22137</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T13:50:17Z</dc:date>
    </item>
    <item>
      <title>Re: skip certain fields</title>
      <link>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286205#M22138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Chad&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The following will deal with the blanks within your date fields:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def fnElapsed(STRDTTM, ENDDTTM):
&amp;nbsp; from datetime import datetime
&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateStart&amp;nbsp; = datetime.strptime(STRDTTM, "%d/%m/%Y %I:%M:%S %p")
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateEnd = datetime.strptime(ENDDTTM, "%d/%m/%Y %I:%M:%S %p")
&amp;nbsp;&amp;nbsp;&amp;nbsp; timeDiff = dateEnd - dateStart
&amp;nbsp;&amp;nbsp;&amp;nbsp; elapMin = timeDiff.total_seconds()/60
&amp;nbsp;&amp;nbsp;&amp;nbsp; return elapMin
&amp;nbsp; except Exception as inst:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print type(inst)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;fnElapsed(!STRDTTM!, !ENDDTTM!)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG __jive_id="195956" alt="PythonCodeBlock.png" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/195956_PythonCodeBlock.png" style="height: auto;" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:50:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286205#M22138</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2021-12-11T13:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: skip certain fields</title>
      <link>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286206#M22139</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In answer to your original question:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d; font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;I am wondering if there is a quick python addition I can add that when it encounters a datetime field with no time, it will skip over that record and keep running through the records. &lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def fnElapsed(STRDTTM, ENDDTTM): 
&amp;nbsp; from datetime import datetime
&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateStart = datetime.strptime(STRDTTM, "%m/%d/%Y %I:%M:%S %p") 
&amp;nbsp;&amp;nbsp;&amp;nbsp; dateEnd = datetime.strptime(ENDDTTM, "%m/%d/%Y %I:%M:%S %p") 
&amp;nbsp;&amp;nbsp;&amp;nbsp; timeDiff = dateEnd - dateStart 
&amp;nbsp;&amp;nbsp;&amp;nbsp; elapMin = timeDiff.total_seconds()/60 
&amp;nbsp;&amp;nbsp;&amp;nbsp; return elapMin
&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:50:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/skip-certain-fields/m-p/286206#M22139</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T13:50:22Z</dc:date>
    </item>
  </channel>
</rss>

