Python Forum
ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' (/thread-29461.html)



ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' - rajesh3383 - Sep-03-2020

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>


RE: ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' - Larz60+ - Sep-03-2020

Please show the complete and unedited error traceback.
It contains valuable debugging information.


RE: ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' - rajesh3383 - Sep-03-2020

@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'



RE: ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' - bowlofred - Sep-03-2020

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



RE: ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' - buran - Sep-03-2020

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))