Home > other >  How to append list data to an existing XML file?
How to append list data to an existing XML file?

Time:11-09

I have an existing XML file named Energy1.xml which looks like this:

<Record>
     <RecordID>Energy1</RecordID>
     <Location>London</Location>
     <Datum>07-09-2020</Datum>
     <Time>07u43m55s</Time>
     <Version>2.0.1</Version>
     <Version_2>v1.9</Version_2>
 <Max_30>
     <I_30Hz_1s>56.40</I_30Hz_1s>
     <I_30Hz_2s>7.443</I_30Hz_2s>
 </Max_30>
 <Max_50>
     <I_50Hz_1s>1.56</I_50Hz_1s>
     <I_50Hz_2s>0.363</I_50Hz_2s>
 </Max_50>
</Record>

I also have a list which looks like this:

users_list = [
    ['C8_200', '5624', 'orange', '07-09-2020 8:48:02', 'banana', '3', '2316', 'mango', 'blue', 'yellow', 'red', '07-09-2020 09:08:43', '07-09-2020'], 
    ['C8_200', '5624', 'orange', '07-09-2020 8:48:02', 'banana', '4', '2708', 'mango', 'blue', 'yellow', 'red', '07-09-2020 09:08:43', '07-09-2020'],
    ['C8_200', '414018', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown']]

For every list in user_list my aim is to add index[1], [2], [5] and [6] as new data to the current XML file

Untill now this is my code:

import xml.etree.ElementTree as ET
    
def append_xml():
  
        # we make root element
        usrconfig = ET.Element("Record")
  
        # create sub element
        usrconfig = ET.SubElement(usrconfig, "Record")

        with open('Energy1.xml', 'a') as output:
               
  
append_xml()

I want my XML-file to look this:

<Record>
        <RecordID>Energy1-07u43m55s</RecordID>
        <Location>London</Location>
        <Datum>07-09-2020</Datum>
        <Time>07u43m55s</Time>
        <Version>2.0.1</Version>
        <Version_2>v1.9</Version_2>
    <Max_30e_perc_25Hz>
        <I_25Hz_1s>56.4059624876772</I_25Hz_1s>
        <I_25Hz_2s>7.44349841777878</I_25Hz_2s>
    </Max_30e_perc_25Hz>
    <Max_30e_perc_75Hz>
        <I_75Hz_1s>1.56101496700227</I_75Hz_1s>
        <I_75Hz_2s>0.363494028934492</I_75Hz_2s>
    </Max_30e_perc_75Hz>
    <Row>
        <RecordID>5624</RecordID>
        <Fruit>orange</Fruit>
        <Type>banana</Type>
        <Number>3</Number>
    <Row>
    <Row>
        <RecordID>5624</RecordID>
        <Fruit>mango</Fruit>
        <Type>apple</Type>
        <Number>4</Number>
    <Row>
    <Row>
        <RecordID>414018</RecordID>
        <Fruit>unkown</Fruit>
        <Type>unkown</Type>
        <Number>unkown</Number>
    <Row>
</Record>

CodePudding user response:

try the below

import xml.etree.ElementTree as ET


xml = '''<Record>
     <RecordID>Energy1</RecordID>
     <Location>London</Location>
     <Datum>07-09-2020</Datum>
     <Time>07u43m55s</Time>
     <Version>2.0.1</Version>
     <Version_2>v1.9</Version_2>
 <Max_30>
     <I_30Hz_1s>56.40</I_30Hz_1s>
     <I_30Hz_2s>7.443</I_30Hz_2s>
 </Max_30>
 <Max_50>
     <I_50Hz_1s>1.56</I_50Hz_1s>
     <I_50Hz_2s>0.363</I_50Hz_2s>
 </Max_50>
</Record>'''

users_list = [
    ['C8_200', '5624', 'orange', '07-09-2020 8:48:02', 'banana', '3', '2316', 'mango', 'blue', 'yellow', 'red', '07-09-2020 09:08:43', '07-09-2020'], 
    ['C8_200', '5624', 'orange', '07-09-2020 8:48:02', 'banana', '4', '2708', 'mango', 'blue', 'yellow', 'red', '07-09-2020 09:08:43', '07-09-2020'],
    ['C8_200', '414018', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown', 'unkown']]
idx_lst = {1:"Records",2:"Fruit",5:"Type",6:"Number"}   
root = ET.fromstring(xml)
for lst in users_list:
  row = ET.SubElement(root, 'Row')
  for idx,name in idx_lst.items():
    x = ET.SubElement(row,name)
    x.text = lst[idx]
ET.dump(root)
tree = ET.ElementTree(root)
tree.write('output.xml')

output

<?xml version="1.0" encoding="UTF-8"?>
<Record>
   <RecordID>Energy1</RecordID>
   <Location>London</Location>
   <Datum>07-09-2020</Datum>
   <Time>07u43m55s</Time>
   <Version>2.0.1</Version>
   <Version_2>v1.9</Version_2>
   <Max_30>
      <I_30Hz_1s>56.40</I_30Hz_1s>
      <I_30Hz_2s>7.443</I_30Hz_2s>
   </Max_30>
   <Max_50>
      <I_50Hz_1s>1.56</I_50Hz_1s>
      <I_50Hz_2s>0.363</I_50Hz_2s>
   </Max_50>
   <Row>
      <Records>5624</Records>
      <Fruit>orange</Fruit>
      <Type>3</Type>
      <Number>2316</Number>
   </Row>
   <Row>
      <Records>5624</Records>
      <Fruit>orange</Fruit>
      <Type>4</Type>
      <Number>2708</Number>
   </Row>
   <Row>
      <Records>414018</Records>
      <Fruit>unkown</Fruit>
      <Type>unkown</Type>
      <Number>unkown</Number>
   </Row>
</Record>
  • Related