Export CSV to XML using Python Pandas

In this tutorial, we’ll explore different techniques for exporting your CSV files or text into XML format.

From basic conversions to more complex cases like handling nested data and exporting to XML files, each example is designed to address different challenges you may encounter in data processing.

 

 

Reading CSV Data from a File

You can read the CSV file using Pandas read_csv() function then you can export it to XML using Pandas to_xml()

Assuming the CSV file content is:

ID,Name,Age
4,User4,30
5,User5,25
6,User6,40

Code:

import pandas as pd
csv_file = 'users.csv'
df = pd.read_csv(csv_file)
xml_data = df.to_xml()
print(xml_data)

Output:

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <ID>4</ID>
    <Name>User4</Name>
    <Age>30</Age>
  </row>
  <row>
    <index>1</index>
    <ID>5</ID>
    <Name>User5</Name>
    <Age>25</Age>
  </row>
  <row>
    <index>2</index>
    <ID>6</ID>
    <Name>User6</Name>
    <Age>40</Age>
  </row>
</data>

 

Exporting Converted XML Data to an XML File

You can save the exported XML to a file by specifying the file path to the to_xml() function:

import pandas as pd
csv_file = 'users.csv'
df = pd.read_csv(csv_file)
xml_data = df.to_xml("output.xml")
print("XML data has been saved to output.xml")

Output:

XML data has been saved to output.xml

In this example, we first create a simple DataFrame. Then, we use to_xml() function of the DataFrame to convert it into XML format.

 

Excluding Index from XML

You can set the index parameter to False to exclude the index from the output:

import pandas as pd
csv_file = 'users.csv'
df = pd.read_csv(csv_file)
xml_data = df.to_xml(index=False)
print(xml_data)

Output:

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <ID>4</ID>
    <Name>User4</Name>
    <Age>30</Age>
  </row>
  <row>
    <ID>5</ID>
    <Name>User5</Name>
    <Age>25</Age>
  </row>
  <row>
    <ID>6</ID>
    <Name>User6</Name>
    <Age>40</Age>
  </row>
</data>

 

Customizing Root and Row Tags

You can customize the XML root and row tags for a more descriptive XML structure.

import pandas as pd
csv_file = 'users.csv'
df = pd.read_csv(csv_file)
xml_data = df.to_xml(root_name='Users', row_name='User')
print(xml_data)

Output:

<?xml version='1.0' encoding='utf-8'?>
<Users>
  <User>
    <index>0</index>
    <ID>7</ID>
    <Name>User7</Name>
    <Age>29</Age>
  </User>
  <User>
    <index>1</index>
    <ID>8</ID>
    <Name>User8</Name>
    <Age>31</Age>
  </User>
  <User>
    <index>2</index>
    <ID>9</ID>
    <Name>User9</Name>
    <Age>22</Age>
  </User>
</Users>

 

Specify Columns

You can specify the columns you want to export to XML by using attr_cols parameter:

import pandas as pd
csv_file = 'users.csv'
df = pd.read_csv(csv_file)
xml_data = df.to_xml(attr_cols=['ID'])
print(xml_data)

Output:

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row index="0" ID="4"/>
  <row index="1" ID="5"/>
  <row index="2" ID="6"/>
</data>

 

Filtering Data Before Conversion

In this example, we filter the DataFrame based on specific criteria before converting it to XML.

Assume this is the CSV file:

ID,Name,Age,Membership
16,User16,20,Yes
17,User17,30,No
18,User18,40,Yes
19,User19,50,No

Code:

import pandas as pd
csv_file = 'users.csv'
df = pd.read_csv(csv_file)
filtered_df = df[df['Membership'] == 'Yes']
xml_data = filtered_df.to_xml()
print(xml_data)

Output:

<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <ID>16</ID>
    <Name>User16</Name>
    <Age>20</Age>
    <Membership>Yes</Membership>
  </row>
  <row>
    <index>2</index>
    <ID>18</ID>
    <Name>User18</Name>
    <Age>40</Age>
    <Membership>Yes</Membership>
  </row>
</data>
Leave a Reply

Your email address will not be published. Required fields are marked *