Login
Order Now
Support
Python Task on Parsing And Manipulating Xml Data

Python Task on Parsing And Manipulating Xml Data

  • 27th Oct, 2021
  • 21:59 PM

# Calling arguments: plant_catalog.xml plantName percentChange
import xml.etree.ElementTree as ET
import sys

# input parameters
searchName = sys.argv[2]
percent = float(sys.argv[3])

# parse XML data file
tree = ET.parse(sys.argv[1])
root = tree.getroot()

# change rate
change_rate = (percent + 100) / 100

for plant in root.findall("./COMMON"):
    if plant.text == searchName:
        new_price = float(root.find("./PRICE").text)
        new_price = str(round((new_price * change_rate),2)) 
        root.find("./PRICE").text = new_price
tree.write('output.xml')  

Share this post

assignment helpassignment helperassignment expertsassignment writing services