CSV to XML – How to Convert in Python?

Problem Formulation

python csv to xml

Input: You have some data in a CSV file stored in 'my_file.csv' where the first row is the header and the remaining rows are values associated to the column names in the header.

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000

Desired Output: You want to store the data in an XML file 'my_file.xml' so that each row is represented by an XML <row> tag and each column value is associated with a specific column header tag.

<data>
    <row id='Alice'>
        <Name>Alice</Name>
        <Job>Programmer</Job>
        <Age>23</Age>
        <Income>110000</Income>
    </row>
    <row id='Bob'>
        <Name>Bob</Name>
        <Job>Executive</Job>
        <Age>34</Age>
        <Income>90000</Income>
    </row>
    <row id='Carl'>
        <Name>Carl</Name>
        <Job>Sales</Job>
        <Age>45</Age>
        <Income>50000</Income>
    </row>
</data>

Python CSV to XML – Basic Example

You can convert a CSV to an XML using the following approach:

  • Read the whole CSV file into your Python script.
  • Store the first row as header data that is needed to name your custom XML tags (e.g., <Name>, <Job>, <Age>, and <Income> in our example).
  • Create a function convert_row() that converts each row separately to an XML representation of that row using basic string formatting.
  • Iterate over the data row-wise using csv.reader() and convert each CSV row to XML using your function convert_row().

Here’s the code for copy&paste:

# Convert CSV file to XML string
import csv


filename = 'my_file.csv'

def convert_row(headers, row):
    s = f'<row id="{row[0]}">\n'
    for header, item in zip(headers, row):
        s += f'    <{header}>' + f'{item}' + f'</{header}>\n'
    return s + '</row>'


with open(filename, 'r') as f:
    r = csv.reader(f)
    headers = next(r)
    xml = '<data>\n'

    for row in r:
        xml += convert_row(headers, row) + '\n'

    xml += '</data>'
    print(xml)

Output:

<data>
<row id="Alice">
    <Name>Alice</Name>
    <Job>Programmer</Job>
    <Age>23</Age>
    <Income>110000</Income>
</row>
<row id="Bob">
    <Name>Bob</Name>
    <Job>Executive</Job>
    <Age>34</Age>
    <Income>90000</Income>
</row>
<row id="Carl">
    <Name>Carl</Name>
    <Job>Sales</Job>
    <Age>45</Age>
    <Income>50000</Income>
</row>
</data>

Yay!

Note that instead of printing to the shell, you could print it to a file if this is what you need. Here’s how:

🌍 Learn More: How to print() to a file in Python?

Pandas CSV to XML

You can also use pandas instead of the csv module to read the CSV file into your Python script. Everything else remains similar—I highlighted the lines that have changed in the following code snippet:

import pandas as pd


def convert_row(headers, row):
    s = f'<row id="{row[0]}">\n'
    for header, item in zip(headers, row):
        s += f'    <{header}>' + f'{item}' + f'</{header}>\n'
    return s + '</row>'


df = pd.read_csv("my_file.csv")
headers = df.columns.tolist()
xml = '<data>\n'

for _, row in df.iterrows():
    xml += convert_row(headers, row) + '\n'

xml += '</data>'
print(xml)

More Python CSV Conversions

🐍 Learn More: I have compiled an “ultimate guide” on the Finxter blog that shows you the best method, respectively, to convert a CSV file to JSON, Excel, dictionary, Parquet, list, list of lists, list of tuples, text file, DataFrame, XML, NumPy array, and list of dictionaries.

Related CSV Conversion Tutorials