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
  Python date format changes to date & time 1418 4 512 Jan-20-2024, 04:45 AM
Last Post: 1418
  Export data from PDF as tabular format zinho 5 645 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,797 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,559 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  What time format is this? wrybread 3 1,989 Jun-15-2022, 02:46 PM
Last Post: snippsat
  Real time data satyanarayana 3 20,106 Feb-16-2022, 07:46 AM
Last Post: satyanarayana
  Need Help writing data into Excel format ajitnayak87 8 2,436 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
  How to read rainfall time series and insert missing data points MadsM 4 2,123 Jan-06-2022, 10:39 AM
Last Post: amdi40
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,195 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 2,251 Aug-17-2020, 03:44 PM
Last Post: bearcats6001

Forum Jump:

User Panel Messages

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