简体   繁体   English

读取和更新 XML 中的字母数字值并生成新的 output.xml 文件时出现问题

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

My XML is looks like this我的 XML 看起来像这样

<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. 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.如果我为 PolicyID def splitString(self):单独运行代码,并且 TransactionDate def updateLastModified(self)代码按预期工作。 But when I merge both of these codes and parse this xml, only Transaction date node works as expected.但是当我合并这两个代码并解析这个 xml 时,只有交易日期节点按预期工作。 PolicyId changes are not reflects and output XML not generates. PolicyId 更改不会反映,并且 output XML 不会生成。

Sorry, no clew where I am making mistake.对不起,没有提示我在哪里犯错。 Am I doing something wrong while print def splitString(self) ?我在 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 .您能否帮助修复 policyid 节点并生成新的 output xml... 要生成新的 output 文件,我使用的是self.tree.write('output.xml')错误NameError: name 'self' is not defined name'self.xml) I have this defined as self.tree = ET.parse(r'C:\Users\\\Desktop\XML\python.xml') .我将其定义为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.根据给定的 XML,节点“”不会从“ABC000002SDF”更新为“ABCSDF000002”。 and node is expected to change as 2020-08-23 15:29:14和节点预计会改变为 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__ .考虑让第二种方法解析更新 XML 而不是覆盖在__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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 机器人框架中的output.xml文件是如何生成的 - How is the output.xml file generated in robot framework 在python中生成xml文件时,output中没有空格 - While generating an xml file in python, there are no spaces in output Allure 无法读取由机器人框架中的 AllureReportLibrary 生成的 output.xml 文件 - Allure not able to read output.xml file generated by AllureReportLibrary in Robot Framework 如何从 Robot Framework 输出更改 output.xml 的结构 - How to change the structure of output.xml from Robot Framework output 在 Robot Framework 中的 Suite Teardown 之前解析 output.xml - Parse output.xml before Suite Teardown in Robot Framework Python:如何使用iterparse方法读取和更新xml并保存到新的xml文件中? - Python: how to update the xml and save to a new xml file, with iterparse method reading and updating? 基于 dataframe 生成带有条件 output 的 XML 文件 - Generating an XML file with conditional output based on a dataframe 从python minidom生成xml文件时出现问题 - Issue in generating the xml file from python minidom 如何在log.html和output.xml中向ROBOT Framework测试统计信息添加一些外部链接? - How to add some external links to ROBOT Framework Test Statistics in log.html and output.xml? 读取XML文件可能留下新行 - Reading XML file leaving possible new lines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM