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


Messages In This Thread
ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' - by rajesh3383 - Sep-03-2020, 04:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python date format changes to date & time 1418 4 618 Jan-20-2024, 04:45 AM
Last Post: 1418
  Export data from PDF as tabular format zinho 5 708 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,887 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,670 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  What time format is this? wrybread 3 2,090 Jun-15-2022, 02:46 PM
Last Post: snippsat
  Real time data satyanarayana 3 24,251 Feb-16-2022, 07:46 AM
Last Post: satyanarayana
  Need Help writing data into Excel format ajitnayak87 8 2,541 Feb-04-2022, 03:00 AM
Last Post: Jeff_t
  How to read rainfall time series and insert missing data points MadsM 4 2,187 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,318 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 2,294 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