Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given a csv file of different instances, how do I convert each instance to a xml file?

Given :

Instantiation, var_a,var_b
1,state0,state1
2,state0,state0
3,state1,state1
4,state1,state0

Output :
4 xml files with different names, each should look like:

<?xml version="1.0" encoding="UTF-8"?>
<instantiation date="Aug 25, 2006 9:50:50 AM">
<inst id="var_a" value="state0"/>
<inst id="var_b" value="state1"/>
</instantiation>


What I have tried:

I have managed to write the output in one text file but that is not a solution for further steps. Help me if there is a way to create multiple text files from a single python script.
Posted
Updated 8-May-22 3:46am
Comments
OriginalGriff 7-May-22 3:35am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project. So we have no idea what part of this is giving you difficulties: we don't know what you have done to read the CSV, where it is stored, or what part of outputting it as separate files is giving you difficulties. In short, we don't know what help you actually need, or what you expect us to do for you!

Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 7-May-22 4:36am    
You just nee to create a function that takes a string and creates an XML output file as per your requirement. You then call the function for each line of the csv input.

1 solution

Steps to do:
1. create dataframe from csv data: pandas.read_csv — pandas 1.4.2 documentation[^]
2. convert dataframe into xml: pandas.DataFrame.to_xml — pandas 1.4.2 documentation[^]

In case you want to export each row (of csv data) into separate xml file, loop through the rows of dataframe. See: pandas.DataFrame.iterrows — pandas 1.4.2 documentation[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900