Python Forum
ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f'
#1
For the node 'TransactionDate' i have a logic before updating it for policy"POL000002NGJ". The logic i am trying to implement is If existing 'TransactionDate' < than today, then add 5 days with current value and parse it to xml.

Transaction Date Format in XML : 2020-03-23T10:56:15.00

Please Note that, If i parsing the DateTime value like below, It works good But i dont want to hardcode the value... I want to Parse it as a string object to handle for any datetime in format ""%Y-%m-%dT%H:%M:%S.%f""...

# <TransactionDate> 
today = datetime.now()
TransactionDate = doc.find('TransactionDate')
Date = '2020-03-24T10:56:15.00' 
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")

if previous_update < today:
     today = previous_update - timedelta(days=-5)
     TransactionDate = today.strftime("%Y-%m-%dT%H:%M:%S.%f")
Below code while parsing it as a DateTime Object, I have an issue.. I got struck here and referred other answers in online forums, But still i got struct up here and unable to resolve the issue...

if any help to fix will be a great helpful. Thanks. Below code using lxml and getting help to support below code will helpful. Because i already completed for other nodes. My understanding is Date variable is calling as None. but i have the object defined as "Date = str(TransactionDate)". But struck here to fix.. Please help..

# <TransactionDate> 
today = datetime.now()
TransactionDate = doc.find('TransactionDate')
Date = str(TransactionDate)
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")

if previous_update < today:
     today = previous_update - timedelta(days=-5)
     TransactionDate = today.strftime("%Y-%m-%dT%H:%M:%S.%f")
Full Code is Below

from lxml import etree
from datetime import datetime, timedelta
import random, string


doc = etree.parse(r'C:\Users\python.xml') 

# <PolicyId> - Random generated policy number
Policy_Random_Choice = 'POL' + ''.join(random.choices(string.digits, k=6)) + 'NGJ'

# <TransactionDate> 
today = datetime.now()
TransactionDate = doc.find('TransactionDate')
Date = str(TransactionDate)  
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")

if previous_update < today:
     today = previous_update - timedelta(days=-5)
     TransactionDate = today.strftime("%Y-%m-%dT%H:%M:%S.%f")
                                  
#Parsing the Variables
replacements = [Policy_Random_Choice  , TransactionDate ]

targets = doc.xpath('//ROW[PolicyId="POL000002NGJ"]')
for target in targets:
    target.xpath('./PolicyId')[0].text = replacements[0]
    target.xpath('.//TransactionDate')[0].text = replacements[1]
 
print(etree.tostring(doc).decode())
Sample XML

<TABLE>
<ROW>
<PolicyId>POL000002NGJ</PolicyId>
<BusinessCoverageCode>COV00002D3X1</BusinessCoverageCode>
<TransactionDate>2020-03-23T10:56:15.00</TransactionDate>
</ROW>
<ROW>
<PolicyId>POL111111NGJ</PolicyId>
<BusinessCoverageCode>COV00002D3X4</BusinessCoverageCode>
<TransactionDate>2020-03-23T10:56:15.00</TransactionDate>
</ROW>
</TABLE>
Reply
#2
Please show the complete and unedited error traceback.
It contains valuable debugging information.
Reply
#3
@Larz60+ Sorry for not placing the complete error, Please find below

Error:
runfile('C:/Users/relangovan/Desktop/Project_Document/XML/untitled44.py', wdir='C:/Users/relangovan/Desktop/Project_Document/XML') Traceback (most recent call last): File "C:\Users\relangovan\Desktop\Project_Document\XML\untitled44.py", line 26, in <module> previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f") File "C:\Users\relangovan\Anaconda3\lib\_strptime.py", line 568, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) File "C:\Users\relangovan\Anaconda3\lib\_strptime.py", line 349, in _strptime raise ValueError("time data %r does not match format %r" % ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f'
Reply
#4
Quote:My understanding is Date variable is calling as None. but i have the object defined as "Date = str(TransactionDate)"

It's not None the Nonetype value, it's the string 'None'. Presumably TransactionDate isn't found, and is assigned None, which you then turn into a string and assign to Date.

TransactionDate = doc.find('TransactionDate')  # not found, TransactionDate set to None
Date = str(TransactionDate)                    # Date set to the str: 'None'
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f") # Date of 'None' not a valid date
Reply
#5
for row in doc.findall('ROW'):
    transaction_date = row.find('TransactionDate').text
    previous_update = datetime.strptime(transaction_date, "%Y-%m-%dT%H:%M:%S.%f")
    print(previous_update, type(previous_update))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 1,406 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 917 Aug-17-2020, 03:44 PM
Last Post: bearcats6001
  how to retain time format in df.to_csv Mekala 2 1,191 Aug-07-2020, 07:04 AM
Last Post: buran
  Find data using a period of time in SQLITE3 SmukasPlays 2 819 Jul-30-2020, 02:02 PM
Last Post: SmukasPlays
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 885 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  Bode plot from time series experiment data discus 4 2,390 Jun-20-2020, 07:46 AM
Last Post: discus
  Receiving ValueError("bad input shape {0}".format(shape)) error SuryaCitizen 2 1,722 Jun-01-2020, 06:45 AM
Last Post: pyzyx3qwerty
  getting error ValueError: time data '' does not match format '%H:%M' srisrinu 2 3,056 Apr-09-2020, 11:12 AM
Last Post: srisrinu
  Help Graphing Arduino Data Real Time in Python nschulz 0 1,301 Mar-12-2020, 06:15 PM
Last Post: nschulz
  Saves the data in the wrong format Hennie 1 927 Jan-01-2020, 03:40 PM
Last Post: sandeep_ganga

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020