1

I am relatively new to Python and wanted to convert .csv files to XML. Is there a Python module / script which can help in creating this generic?

Though I have already to created custom script which process csv file to and convert them to XML, but was wondering if it can be created to a generic script?

I expect the process to work as "python_script.py Input_XSD_file.xsd Input_CSV_File.csv"

and it creates relevant XML files.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Utkarsh
  • 11
  • 4
  • Do you want to know how to modify your script to accept arguments, or how to change your code so that it works with any xsd / csv file? – Burhan Khalid Apr 08 '19 at 11:14
  • @BurhanKhalid: I want to create a generic Python script which accepts an XSD and creates a corresponding XML file, the data in the XML file can be loaded via a csv file or a database. – Utkarsh Apr 08 '19 at 13:53

1 Answers1

0

You could use the pandas module (which is an important module to know if you are getting into python for data analysis). You create a pandas dataframe from a csv as follows:

    import pandas as pd
    my_df = pd.read_csv('data.csv')

Then you can convert your dataframe into XML using the answer to this question : How do convert a pandas/dataframe to XML?

Orysza
  • 574
  • 1
  • 5
  • 10
  • Thanks for the update @Orysza, but I was looking for a way to make it a generic process where the XML is created from an XSD passed as a **parameter**. But as per the link you helped with is creating an _hard-coded_ XML structure. – Utkarsh Apr 08 '19 at 13:50