Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] CSV to XML – How to Convert in Python?
1
CSV to XML – How to Convert in Python?

4/5 – (1 vote)

Problem Formulation

[Image: csv_to_xml-1024x576.jpg]

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

Code:
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
Code:
'my_file.xml'
so that each row is represented by an XML
Code:
<row>
tag and each column value is associated with a specific column header tag.

Code:
<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.,
    Code:
    <Name>
    ,
    Code:
    <Job>
    ,
    Code:
    <Age>
    , and
    Code:
    <Income>
    in our example).

  • Create a function
    Code:
    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
    Code:
    csv.reader()
    and convert each CSV row to XML using your function
    Code:
    convert_row()
    .

Here’s the code for copy&paste:

Code:
# 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:

Code:
<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:

[Image: 1f30d.png] Learn More: How to
Code:
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:

Code:
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)
Related CSV Conversion Tutorials
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: