Sep-03-2020, 04:01 PM
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""...
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..
<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>
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>