Question

Python Coding

Naveen is owning an electronics shop. He has an XML file that contains all the products that is present in his shop. He wants to tabulate his items in the excel. He found out that a CSV file can be easily represented as an excel. Write a program to help naveen by reading an XML file and printing it in CSV format with heading.


click here to download the XML file



Input Format:


Input is a XML file. (file name should be product.xml). The file is already been created in the platform.


Example:


<productList>


<product>


<id>1</id>


<productName>Mobile</productName>


<cost>6000</cost>


<weight>200</weight>


</product>


<product>


<id>2</id>


<productName>Laptop</productName>


<cost>60000</cost>


<weight>2000</weight>


</product>


</productList>





Output Format:


Print the data in CSV format which is in XML file.(file name should be product.csv)


Example:


id,productName,cost,weight


1,Mobile,6000,200


2,Laptop,60000,2000


0 0
Add a comment Improve this question Transcribed image text
Answer #1

file=open("File.xml", 'r')


message = "id,productName,cost,weight\n"


for data in file:

    if data.strip().__contains__("<id>"):

        message+=data.strip().split(">")[1].split("<")[0]

    if data.strip().__contains__("<productName>"):

        message += ","+data.strip().split(">")[1].split("<")[0]

    if data.strip().__contains__("<cost>"):

        message += ","+data.strip().split(">")[1].split("<")[0]

    if data.strip().__contains__("<weight>"):

        message += "," + data.strip().split(">")[1].split("<")[0]+"\n"


fw=open("product.csv",'w')

fw.write(message)

fw.close()

file.close()



#save File.xml with this data given below and then run the program

'''<productList>

<product>

<id>1</id>

<productName>Mobile</productName>

<cost>6000</cost>

<weight>200</weight>

</product>

<product>

<id>2</id>

<productName>Laptop</productName>

<cost>60000</cost>

<weight>2000</weight>

</product>

</productList>'''


answered by: Satyam Keshri
Add a comment
Know the answer?
Add Answer to:
Python Coding
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Python and in one file please. (Simple functions with an expressions) Create a function called...

    In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...

  • In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filenam...

    In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...

  • Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be...

    Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book tit le, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an...

  • Don't attempt if you can't attempt fully, i will dislike and negative comments would be given...

    Don't attempt if you can't attempt fully, i will dislike and negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book titnle, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an HTML web...

  • the OOP project is needed to be coded in netbeans and store a information in file...

    the OOP project is needed to be coded in netbeans and store a information in file on local machine it could be stored with xml tags since its easier. OOP Project Description Requirements: You are to design and build software media rental system. The software will track each user’s account with its rentals. A user can rent multiple media of different type/genre and all that user’s rentals are managed under their account (single account per user). A user will be...

  • 23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4...

    23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same...

  • Requires Python to answer A client wishes to keep track of his investment in shares. Write...

    Requires Python to answer A client wishes to keep track of his investment in shares. Write a program to help him manage his stock portfolio. You are to record both the shares that he is holding as well as shares that he has sold. For shares be is curreatly holding, record the following data: .a 3-character share code, share name, last purchase date, volume currently held and average purchase price (refer to description under part cii) option 2) For shares...

  • Project 3 Instructions 1. Due Date & Time: 09-26-2020 at 11:59 PM WHAT TO SUBMIT Submit...

    Project 3 Instructions 1. Due Date & Time: 09-26-2020 at 11:59 PM WHAT TO SUBMIT Submit 1 zip file containing 4 files below to iLearn by the deadline. [30 points] ● 3 JAVA Files: TableBmiPro.java, MyOwnIdea.java. DiceRoll.java [24 points] ● 1 File: Make a document that shows the screen captures of execution of your programs and learning points in Word or PDF. Please make sure you capture at least 2 executions for 1 program, so 6 screen captures and one...

  • Bexter Labs produces three products: A, B, and C. Bexter can sell up to 2000 units...

    Bexter Labs produces three products: A, B, and C. Bexter can sell up to 2000 units of Product A, up to 3000 units of Product B, and up to 5000 units of Product C. Each unit of Product C uses two units of A and three units of B and incurs $7 in processing costs. Products A and B are produced from either raw material 1 or raw material 2. It costs $9 to purchase and process one unit of...

  • Java Inventory Management Code Question

    Inventory ManagementObjectives:Use inheritance to create base and child classesUtilize multiple classes in the same programPerform standard input validationImplement a solution that uses polymorphismProblem:A small electronics company has hired you to write an application to manage their inventory. The company requested a role-based access control (RBAC) to increase the security around using the new application. The company also requested that the application menu must be flexible enough to allow adding new menu items to the menu with minimal changes. This includes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT