简体   繁体   中英

Issue while reading and updating an alphanumeric value in XML and generating new output.xml file

My XML is looks like this

<TABLE>
   <ROW>
      <PolicyId updated="yes">ABC000002SDF</PolicyId>
      <ToLimit>1000000</ToLimit>
      <Transaction>CAN</Transaction>
      <DecisionDate>2020-03-23</DecisionDate>
      <WrittenPremium>-1727.55</WrittenPremium>
      <surcharge>0</surcharge>
      <TransactionDate>2020-08-21 15:29:14</TransactionDate>
   </ROW>
</TABLE>

I am trying to parse this XML and read and update the nodes PolicyId (alphanumeric value) & TransactionDate (date time type) and save back to the new output xml includes with the other nodes from the original xml with same value.

If I run the code separately for PolicyID def splitString(self): and TransactionDate def updateLastModified(self) code works as expected. But when I merge both of these codes and parse this xml, only Transaction date node works as expected. PolicyId changes are not reflects and output XML not generates.

Sorry, no clew where I am making mistake. Am I doing something wrong while print def splitString(self) ? Can you please help fixing policyid node and generate new output xml... To generate the new output file, I am using self.tree.write('output.xml') have an error NameError: name 'self' is not defined . I have this defined as self.tree = ET.parse(r'C:\Users\\\Desktop\XML\python.xml') .

Below is the entire code i am using:

import xml.etree.ElementTree as ET
from datetime import datetime

class TimestampUpdater(object):

    def __init__(self, filepath):
        self.meta_file = filepath
        self.tree = ET.parse(r'C:\Users\\\Desktop\XML\python.xml')

    def getMetadataTree(self):
        return self.tree

    def getMetadataRoot(self):
        return self.tree.getroot()
    
    def splitString(self):
        for ROW in self.getMetadataRoot().findall('ROW'): ##
            PolicyId = ROW.find('PolicyId')
            #new_rank = int(PolicyId.text) + 1
            #PolicyId.text = str(int(new_rank))
  
            alpha = "" 
            num = "" 
            special = "" 
            for i in range(len(PolicyId)): 
                if (str[i].isdigit()): 
                    num = num+ str[i] 
                elif((str[i] >= 'A' and str[i] <= 'Z') or
                     (str[i] >= 'a' and str[i] <= 'z')): 
                    alpha += str[i] 
                else: 
                    special +=str[i]
                    
            PolicyId.set('updated', 'yes')
            self.getMetadataTree().write(self.meta_file)
          
            #print(alpha) 
            #print(num)
            #print(special) 
            print(alpha+num+special)

     
    def updateLastModified(self):
            today = datetime.now()
            for ROW in self.getMetadataRoot().findall('ROW'): ##
                TransactionDate = ROW.find('TransactionDate')
                previous_update = datetime.strptime(TransactionDate.text, '%Y-%m-%d %H:%M:%S')
                if previous_update < today:
                    TransactionDate.text = today.strftime('%Y-%m-%d %H:%M:%S')
                    self.getMetadataTree().write(self.meta_file)
         
def print_file_content(filename):
    """Print contents of a file"""
    with open(filename, 'r') as fh:
        for line in fh:
            print(line.rstrip())

if __name__ == '__main__':
    metafile = 'python.xml'
    print("\n====Before updating:====")
    print_file_content(metafile)
    updater = TimestampUpdater(metafile)
    updater.updateLastModified() 
    updater.splitString() 
    print("\n====After updating:====")
    print_file_content(metafile)

self.tree.write('output.xml')

Code is changed based on 2nd Consideration..

I am not getting any exception while running this below code, But the code is not doing what it suppose to be. Node "" not updates to "ABCSDF000002" from "ABC000002SDF" as per the XML given. and node is expected to change as 2020-08-23 15:29:14

import xml.etree.ElementTree as ET
from datetime import datetime

class TimestampUpdater(object):

    def __init__(self, filepath):
        self.meta_file = filepath
        self.tree = ET.parse(r'C:\Users\relangovan\Desktop\Project_Document\XML\python.xml')

    def getMetadataTree(self):
        return self.tree

    def getMetadataRoot(self):
        return self.tree.getroot()
       
    def updateLastModified(self,doc):
            today = datetime.now()
            self.my_tree = ET.parse(doc)
            for ROW in self.my_tree.getroot().findall('ROW'): ##
                TransactionDate = ROW.find('TransactionDate')
                previous_update = datetime.strptime(TransactionDate.text, '%Y-%m-%d %H:%M:%S')
                if previous_update < today:
                    TransactionDate.text = today.strftime('%Y-%m-%d %H:%M:%S')
                    self.my_tree.write(self.meta_file)

    def updatepolicyid(self,doc):
        self.my_tree = ET.parse(doc) 
        for ROW in self.my_tree.getroot().findall('ROW'): ##
            PolicyId = ROW.find('PolicyId')
  
            alpha = "" 
            num = "" 
            special = "" 
            for i in range(len(PolicyId)): 
                if (str[i].isdigit()): 
                    num = num+ str[i] 
                elif((str[i] >= 'A' and str[i] <= 'Z') or
                     (str[i] >= 'a' and str[i] <= 'z')): 
                    alpha += str[i] 
                else: 
                    special +=str[i]
                    
            PolicyId.set('updated', 'yes')
            self.my_tree.write(self.meta_file)
            
def print_file_content(filename):
    """Print contents of a file"""
    with open(filename, 'r') as fh:
        for line in fh:
            print(line.rstrip())

if __name__ == '__main__':
    metafile = 'python.xml'
    print("\n====Before updating:====")
    print_file_content(metafile)
    updater = TimestampUpdater(metafile)
    updater.updateLastModified(r'C:\Users\relangovan\Desktop\Project_Document\XML\python.xml') 
    updater.updatepolicyid(r'C:\Users\relangovan\Desktop\Project_Document\XML\output.xml')
    print("\n====After updating:====")
    print_file_content(metafile)

updater.my_tree.write('output.xml')

Consider having second method parse updated XML instead of overwriting the same tree parsed in __init__ .

def splitString(self, doc): 
    self.tree = ET.parse(doc) 
    for ROW in self.getMetadataRoot().findall('ROW'):
        PolicyId = ROW.find('PolicyId')
        alpha = "" 
        num = "" 
        special = "" 
        for i in range(len(PolicyId)): 
            if (str[i].isdigit()): 
                num = num+ str[i] 
            elif((str[i] >= 'A' and str[i] <= 'Z') or
                 (str[i] >= 'a' and str[i] <= 'z')): 
                alpha += str[i] 
            else: 
                special +=str[i]
                
        PolicyId.set('updated', 'yes')
        self.getMetadataTree().write(self.meta_file)
 
def updateLastModified(self, doc):
    today = datetime.now()
    self.tree = ET.parse(doc) 
    
    for ROW in self.getMetadataRoot().findall('ROW'): ##
        TransactionDate = ROW.find('TransactionDate')
        previous_update = datetime.strptime(TransactionDate.text, '%Y-%m-%d %H:%M:%S')
        if previous_update < today:

            TransactionDate.text = today.strftime('%Y-%m-%d %H:%M:%S')
            self.getMetadataTree().write(self.meta_file)

...

updater = TimestampUpdater(metafile)

updater.updateLastModified('/path/to/original/file.xml') 
updater.splitString(metafile) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM