Fix python script problem

Job ID: 32026201

Budget: $10 – $30 USD

I have a script that collects traffic information from a server via XML. The system name is Datex.
You can read more about Datex here: https://www.datex2.eu/

I got sometimes a problem with the script when the type of the datetime data is changing between %Y-%m-%dT%H:%M:%S.%f or %Y-%m-%dT%H:%M:%S in the XML document that the script is collecting from.

Here is the source in the XML and where the problem is based on.

<situationRecord xsi:type="Accident" id="SE_STA_TRISSID_1_18132419" version="9">
<situationRecordCreationTime>2021-11-06T07:46:44+01:00</situationRecordCreationTime>
<situationRecordVersionTime>2021-11-06T08:10:18.087+01:00</situationRecordVersionTime>
<situationRecordFirstSupplierVersionTime>2021-11-06T08:10:18.087+01:00</situationRecordFirstSupplierVersionTime>


<situationRecord xsi:type="Accident" id="SE_STA_TRISSID_1_18132379" version="11">
<situationRecordCreationTime>2021-11-06T07:16:28.703+01:00</situationRecordCreationTime>
<situationRecordVersionTime>2021-11-06T07:43:37.433+01:00</situationRecordVersionTime>
<situationRecordFirstSupplierVersionTime>2021-11-06T07:43:37.433+01:00</situationRecordFirstSupplierVersionTime>
------------------------------------------------------------------------------------------------------------------------------------------------

Here is the error:
Traceback (most recent call last):
File "clientpull.py", line 215, in <module>
situationRecordCreationTime = get_formatted_date(getTextFromTag(situationRecord.find('./'+ gettagname('situationRecordCreationTime'))))
File "clientpull.py", line 47, in get_formatted_date
dat1 = datetime.datetime.strptime(strs[:-5],"%Y-%m-%dT%H:%M:%S.%f")
File "C:\Python27\lib\_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data '2021-11-06T07:46:44' does not match format '%Y-%m-%dT%H:%M:%S.%f'
------------------------------------------------------------------------------------------------------------------------------------------------
Code:
def get_formatted_date(dat):
strs = dat
strs = strs[::-1].replace(':','',1)[::-1]
print strs
tz = strs[-5:]
print tz
dat1 = datetime.datetime.strptime(strs[:-5],"%Y-%m-%dT%H:%M:%S.%f")
nowtuple = dat1.timetuple()
nowtimestamp = time.mktime(nowtuple)
str1 = email.utils.formatdate(nowtimestamp)
print str1
date_tuple = email.utils.parsedate_tz(str1)
print date_tuple
if date_tuple:
tmp_date = email.utils.mktime_tz(date_tuple)
tmp_date = datetime.datetime.fromtimestamp(tmp_date)
print tmp_date
return ct(tmp_date)
------------------------------------------------------------------------------------------------------------------------------------------------
In the script, I found a post that handles the timestamp differently, but that is not implemented in the script.

def get_formatted_date_nomicro(dat):
strs = dat
strs = strs[::-1].replace(':','',1)[::-1]
print strs
tz = strs[-5:]
print tz
dat1 = datetime.datetime.strptime(strs[:-5],"%Y-%m-%dT%H:%M:%S")
nowtuple = dat1.timetuple()
nowtimestamp = time.mktime(nowtuple)
str1 = email.utils.formatdate(nowtimestamp)
print str1
date_tuple = email.utils.parsedate_tz(str1)
print date_tuple
if date_tuple:
tmp_date = email.utils.mktime_tz(date_tuple)
tmp_date = datetime.datetime.fromtimestamp(tmp_date)
print tmp_date
return ct(tmp_date)
------------------------------------------------------------------------------------------------------------------------------------------------
The script is only calling: get_formatted_date. When it's converting the timestamp. That is the problem.

Your task will be to fix that script so it can handle multiple timestamps and get the error away.
Related categories: XML Python MySQL Data Scraping