A possible solution is to first load the csv into Pandas and then convert it row by row into XML, as so:

import pandas as pd
df = pd.read_csv('untitled.txt', sep='|')

With the sample data (assuming separator and so on) loaded as:

          Title                   Type Format  Year Rating  Stars  \
0  Enemy Behind           War,Thriller    DVD  2003     PG     10   
1  Transformers  Anime,Science Fiction    DVD  1989      R      9   

             Description  
0          Talk about...  
1  A Schientific fiction  

And then converting to xml with a custom function:

def convert_row(row):
    return """<movietitle="%s">
    <type>%s</type>
    <format>%s</format>
    <year>%s</year>
    <rating>%s</rating>
    <stars>%s</stars>
    <description>%s</description>
</movie>""" % (
    row.Title, row.Type, row.Format, row.Year, row.Rating, row.Stars, row.Description)

print '\n'.join(df.apply(convert_row, axis=1))

This way you get a string containing the xml:

<movietitle="Enemy Behind">
    <type>War,Thriller</type>
    <format>DVD</format>
    <year>2003</year>
    <rating>PG</rating>
    <stars>10</stars>
    <description>Talk about...</description>
</movie>
<movietitle="Transformers">
    <type>Anime,Science Fiction</type>
    <format>DVD</format>
    <year>1989</year>
    <rating>R</rating>
    <stars>9</stars>
    <description>A Schientific fiction</description>
</movie>

that you can dump in to a file or whatever.

Inspired by this great answer.


Edit: Using the loading method you posted (or a version that actually loads the data to a variable):

import csv              
f = open('movies2.csv')
csv_f = csv.reader(f)   
data = []

for row in csv_f: 
   data.append(row)
f.close()

print data[1:]

We get:

[['Enemy Behind', 'War', 'Thriller', 'DVD', '2003', 'PG', '10', 'Talk about...'], ['Transformers', 'Anime', 'Science Fiction', 'DVD', '1989', 'R', '9', 'A Schientific fiction']]

And we can convert to XML with minor modifications:

def convert_row(row):
    return """<movietitle="%s">
    <type>%s</type>
    <format>%s</format>
    <year>%s</year>
    <rating>%s</rating>
    <stars>%s</stars>
    <description>%s</description>
</movie>""" % (row[0], row[1], row[2], row[3], row[4], row[5], row[6])

print '\n'.join([convert_row(row) for row in data[1:]])

Getting identical results:

<movietitle="Enemy Behind">
    <type>War</type>
    <format>Thriller</format>
    <year>DVD</year>
    <rating>2003</rating>
    <stars>PG</stars>
    <description>10</description>
</movie>
<movietitle="Transformers">
    <type>Anime</type>
    <format>Science Fiction</format>
    <year>DVD</year>
    <rating>1989</rating>
    <stars>R</stars>
    <description>9</description>
</movie>
Answer from robertoia on Stack Overflow
Top answer
1 of 5
23

A possible solution is to first load the csv into Pandas and then convert it row by row into XML, as so:

import pandas as pd
df = pd.read_csv('untitled.txt', sep='|')

With the sample data (assuming separator and so on) loaded as:

          Title                   Type Format  Year Rating  Stars  \
0  Enemy Behind           War,Thriller    DVD  2003     PG     10   
1  Transformers  Anime,Science Fiction    DVD  1989      R      9   

             Description  
0          Talk about...  
1  A Schientific fiction  

And then converting to xml with a custom function:

def convert_row(row):
    return """<movietitle="%s">
    <type>%s</type>
    <format>%s</format>
    <year>%s</year>
    <rating>%s</rating>
    <stars>%s</stars>
    <description>%s</description>
</movie>""" % (
    row.Title, row.Type, row.Format, row.Year, row.Rating, row.Stars, row.Description)

print '\n'.join(df.apply(convert_row, axis=1))

This way you get a string containing the xml:

<movietitle="Enemy Behind">
    <type>War,Thriller</type>
    <format>DVD</format>
    <year>2003</year>
    <rating>PG</rating>
    <stars>10</stars>
    <description>Talk about...</description>
</movie>
<movietitle="Transformers">
    <type>Anime,Science Fiction</type>
    <format>DVD</format>
    <year>1989</year>
    <rating>R</rating>
    <stars>9</stars>
    <description>A Schientific fiction</description>
</movie>

that you can dump in to a file or whatever.

Inspired by this great answer.


Edit: Using the loading method you posted (or a version that actually loads the data to a variable):

import csv              
f = open('movies2.csv')
csv_f = csv.reader(f)   
data = []

for row in csv_f: 
   data.append(row)
f.close()

print data[1:]

We get:

[['Enemy Behind', 'War', 'Thriller', 'DVD', '2003', 'PG', '10', 'Talk about...'], ['Transformers', 'Anime', 'Science Fiction', 'DVD', '1989', 'R', '9', 'A Schientific fiction']]

And we can convert to XML with minor modifications:

def convert_row(row):
    return """<movietitle="%s">
    <type>%s</type>
    <format>%s</format>
    <year>%s</year>
    <rating>%s</rating>
    <stars>%s</stars>
    <description>%s</description>
</movie>""" % (row[0], row[1], row[2], row[3], row[4], row[5], row[6])

print '\n'.join([convert_row(row) for row in data[1:]])

Getting identical results:

<movietitle="Enemy Behind">
    <type>War</type>
    <format>Thriller</format>
    <year>DVD</year>
    <rating>2003</rating>
    <stars>PG</stars>
    <description>10</description>
</movie>
<movietitle="Transformers">
    <type>Anime</type>
    <format>Science Fiction</format>
    <year>DVD</year>
    <rating>1989</rating>
    <stars>R</stars>
    <description>9</description>
</movie>
2 of 5
3

I tried to generalize robertoia's function convert_row for any header instead of writing it by hand.

import csv  
import pandas as pd
            
f = open('movies2.csv')
csv_f = csv.reader(f)   
data = []

for row in csv_f: 
   data.append(row)
f.close()

df = pd.read_csv('movies2.csv')
header= list(df.columns)

def convert_row(row):
     str_row = """<%s>%s</%s> \n"""*(len(header)-1)
     str_row = """<%s>%s""" +"\n"+ str_row + """</%s>"""
     var_values = [list_of_elments[k] for k in range(1,len(header)) for list_of_elments in [header,row,header]]
     var_values = [header[0],row[0]]+var_values+[header[0]]
     var_values =tuple(var_values)
     return str_row % var_values

text ="""<collection shelf="New Arrivals">"""+"\n"+'\n'.join([convert_row(row) for row in data[1:]])+"\n" +"</collection >"
print(text)
with open('output.xml', 'w') as myfile: 
  myfile.write(text)

Of course with pandas now, it is simpler to just use to_xml() :

df= pd.read_csv('movies2.csv')
with open('outputf.xml', 'w') as myfile: 
  myfile.write(df.to_xml())

🌐
AskPython
askpython.com › home › converting data in csv to xml in python
Converting Data in CSV to XML in Python - AskPython
April 4, 2023 - The first thing to be done in the above code is to import the Pandas module. It would be highly necessary to read the CSV file as a dataframe. The check on the input file as CSV and the output file as XML is done using Python String’s endswith() ...
Discussions

Python CSV to XML converter - Code Review Stack Exchange
I will need to start creating the ... in the CSV file and I believe, in its current state, there is too much duplication. I am attempting to learn Python while using a real user case, please can someone provide any guidance on how I could make the code more eloquent and more DRY. Do I need to be using classes and functions or is my approach OK for the type of file (XML) and creating? import pandas as pd from ... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
October 11, 2010
Read xml column inside csv file with Python
Hi, I am very new in Python and need to read an xml column inside a csv file with Python. I can see some code on google on how to read that csv file but I don’t know how to read the xml column inside the csv file. Can someone help? any idea would be appreciated. Thank you! More on discuss.python.org
🌐 discuss.python.org
0
0
July 22, 2022
Python convert CSV to XML with pandas groupby - Stack Overflow
I have a csv that I need to convert to XML using Python. I'm a novice python dev. Example CSV data: Amount,Code CODE50,1246 CODE50,6290 CODE25,1077 CODE25,9790 CODE100,5319 CODE100,4988 Necessary ... More on stackoverflow.com
🌐 stackoverflow.com
February 7, 2026
Convert CSV to XML by LXML & Pandas in python - Stack Overflow
I'm working on a project that I need to convert a csv to XML. I had the following code works fine, but the result isn't what I'm looking for and I wonder if anyone can point out which method I'm mi... More on stackoverflow.com
🌐 stackoverflow.com
🌐
YouTube
youtube.com › watch
How to Convert CSV to XML Using Python with #tkinter and Pandas | Step-by-Step Guide #dataexport - YouTube
Learn how to convert CSV files to XML format using Python, Tkinter, and Pandas in this step-by-step tutorial. This video covers creating a user-friendly GUI ...
Published   July 22, 2022
Top answer
1 of 1
5

The real issue with repeating yourself here is that you are iterating over your dataframe twice when you don't have to. Fortunately, the devs for lxml make the elements behave like lists, so you can create everything that you need to in one go, then just do root.append(sub_element) at the end like you would with a list.

Whenever you find yourself directly iterating over a pandas dataframe, that's usually a sign that it's not the correct data structure for the task. There are certainly exceptions, but I think using the csv module here might be a bit better of a fit. The reason being that you are technically iterating over the whole file with pd.read_csv, only to iterate again. csv.reader will allow you to iterate and process at the same time.

First, declare all of the trees you might need:

import csv
from lxml import etree as et

# configure your elements here. lxml acts as a list,
# so make them their own trees until you are ready to merge
root = et.Element('SignallingSchemeData', xmlns='boo')
source = et.Element('Source')
view = et.Element('View')

Note that I haven't used the SubElement factory class yet. Since Element can be treated as a list, we can leave the linking of elements to root to the end of the script.

Moving forward:

# you want this inserted first, so SubElement is 
# perfectly fine here
timeframe = et.SubElement(root,'TimeFrame')
timeframe.attrib["fileUID"] = str(uuid.uuid4())
# you don't need the str function call here, it's already a string
timeframe.attrib["name"] = "timeframe 1"

# open your file here
with open('assets.csv') as fh:
    # the separator by default is ','
    # so no need to specify it
    reader = csv.DictReader(fh)

Now, csv.DictReader allows you to keep the row[key] paradigm because it will create an OrderedDict for each row:

with open('assets.csv') as fh:
    reader = csv.DictReader(fh)
    row = next(reader)

row
OrderedDict([('ID', '30734'), ('CODE', 'S1'), ('ELR', 'LEC1'), ('TRID', '1100'), ('DIR', ''), ('MILEAGE', '008+0249 (9-1497)'), ('X', '518169.12'), ('Y', '185128.27'), ('Z', '37.52'), ('DESC', ''), ('IMAGE', 'Asset30734.jpg')])

row['ID']
'30734'

row['CODE']
'S1'

This requires minimal changes from your original script. You can iterate over the reader just like before except the index doesn't have to be there anymore:

with open('assets.csv') as fh:
    reader = csv.DictReader(fh)

    for row in reader:
        # rest of loop

Refactoring the loop

Equipment

First, you can create a function to handle everything that's happening in the if block. This makes the core of the loop more readable, as well as logically separates functions of the code. Overall, the core logic is good. Your variable names are descriptive, if a little lengthy, with the only change really being formatting:

def add_equipment(root, row):
    '''Add equipment elements and supporting child elements to root'''
    equipment = et.SubElement(root, 'Equipment')

    # I'm grouping blocks of code/variables that group together
    signal_equipment = et.SubElement(equipment, 'SignalEquipment')
    signal_equipment.attrib["fileUID"] = row["ID"]
    signal_equipment.attrib["name"] = row["DESC"]

    # The names have been changed to snake_case with lowercased
    # letters, as is the naming conventions of variables and functions
    # in python (but not classes)
    equipment = et.SubElement(root, 'Equipment')
    eq_support = et.SubElement(equipment, 'EquipmentSupportEquipment')
    eq_support.attrib["fileUID"] = str(uuid.uuid4())

    reference = et.SubElement(signal_equipment, 'EquipmentSupportReference').text = eq_support.attrib["fileUID"]

I don't need to return anything because root is being modified in-place by these operations.

Now, the first portion of your loop looks like this:

with open('assets.csv') as fh:
    reader = csv.DictReader(fh)

    for row in reader:
        if row['CODE'] == 'S1':
            # now all of this code sits
            # in a function, and it's much easier to tell what's
            # going on
            add_equipment(root, row)
        else:
            equipment = et.SubElement(root, 'Equipment')

View

The view can be cleaned up in pretty much the same manner. Again, I don't really see anything egregious other than just some logical grouping and formatting. Your code and logical flow looks good, it's clear and easy to read

def add_view(view, row):
    '''Add supporting child elements to view. Pass in row for value extraction'''
    view_list = et.SubElement(view, 'ViewCoordinatesList')

    view_coords = et.SubElement(view_list, 'ViewCoordinates')

    item_file_uid = et.SubElement(view_coords, 'ItemFileUID')
    item_file_uid.attrib['fileUID'] = row['ID']

    longitude = et.SubElement(view_coords, 'ViewCoordinatePair', name='longittude')
    longitude.attrib['Value'] = row['Y']

    latitude = et.SubElement(view_coords, 'ViewCoordinatePair', name='latitude')
    latitude.attrib['Value'] = row['X']

    height = et.SubElement(view_coords, 'ViewCoordinatePair', name='height')
    height.attrib['Value'] = row['Z']

You don't need the str to wrap row['X'] anymore because csv.DictReader doesn't infer datatypes, everything is a string.

The loop as a whole now looks like this:

with open('assets.csv') as fh:
    reader = csv.DictReader(fh)

    for row in reader:
        if row['CODE'] == 'S1':
            # refactor to a function here that
            # adds to root and takes row as an argument
            add_equipment(root, row)
        else:
            equipment = et.SubElement(root, 'Equipment')

        # add the rest of your loop in a function here to
        # deal with the view tree, again, take row as an argument
        add_view(view, row)

Now, your loop can just be read as a loop, and the logic inside the if blocks is abstracted into functions.

Constructing your tree

Now, to get everything in order, it's just a call to root.append:

root.append(source)
root.append(view)

And everything is in order with one pass over the csv file. Your last line can remain perfectly intact:

et.ElementTree(root).write('test.xml', pretty_print=True, xml_declaration=True, encoding='UTF-8', standalone=None)

Timing Results

Original Script

python -m timeit 'import file'
10 loops, best of 3: 0.118 usec per loop

Refactored Script

python -m timeit 'import file2'
10000000 loops, best of 3: 0.0809 usec per loop
🌐
Aspose
products.aspose.com › aspose.cells › python via java › conversion › csv to xml
Python CSV to XML - CSV to XML Converter | products.aspose.com
November 13, 2025 - Aspose Excel. This comprehensive solution provides Python developers with a fully integrated approach to convert CSV to XML format, enabling seamless saving of CSV data into XML format using the Aspose.Cells library, all through efficient and customizable Python code.
Find elsewhere
🌐
Finxter
blog.finxter.com › home › learn python blog › csv to xml – how to convert in python?
CSV to XML - How to Convert in Python? - Be on the Right Side of Change
September 16, 2018 - 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, ...
🌐
GitHub
github.com › SilentJMA › Tradebyte-CSV-To-XML-with-Python
GitHub - SilentJMA/Tradebyte-CSV-To-XML-with-Python: Converting a Tradebyte Panda file from CSV to XML by using Python. · GitHub
Run the Python script using the following command: ... The script will read the source CSV file, transform the data, and generate an XML file named PandaDDMMYYYYHHMMSS.xml DD:Day, MM: Month, YYYY: Year, HH: Hour, MM: Minute, SS: Secondin the same directory.
Author   SilentJMA
🌐
ActiveState
code.activestate.com › recipes › 577423-convert-csv-to-xml
Convert CSV to XML « Python recipes « ActiveState Code
July 5, 2017 - # example CSV file: myData.csv # id,code name,value # 36,abc,7.6 # 40,def,3.6 # 9,ghi,6.3 # 76,def,99 import sys import os import csv if len(sys.argv) != 2: os._exit(1) path=sys.argv[1] # get folder as a command line argument os.chdir(path) csvFiles = [f for f in os.listdir('.') if f.endswith('.csv') or f.endswith('.CSV')] for csvFile in csvFiles: xmlFile = csvFile[:-4] + '.xml' csvData = csv.reader(open(csvFile)) xmlData = open(xmlFile, 'w') xmlData.write('<?xml version="1.0"?>' + "\n") # there must be only one top-level tag xmlData.write('<csv_data>' + "\n") rowNum = 0 for row in csvData: if
🌐
Saturn Cloud
saturncloud.io › blog › converting-complex-xml-files-to-pandas-dataframecsv-in-python
Converting Complex XML Files to Pandas DataFrame/CSV in Python | Saturn Cloud Blog
December 28, 2023 - The first step in converting an XML file to a DataFrame or CSV is parsing the XML file. We’ll use the xml.etree.ElementTree module in Python, which provides a lightweight and efficient API for parsing and creating XML data.
🌐
Finxter
blog.finxter.com › 5-best-ways-to-convert-csv-to-xml-in-python
5 Best Ways to Convert CSV to XML in Python – Be on the Right Side of Change
November 13, 2024 - After reading the CSV using pandas, we iterate over each row and create corresponding XML elements using lxml. Each column in the CSV becomes a tag in the XML. The etree is then used to convert the structure into an XML document, which is written out with nice formatting.
🌐
Python.org
discuss.python.org › python help
Read xml column inside csv file with Python - Python Help - Discussions on Python.org
July 22, 2022 - Hi, I am very new in Python and need to read an xml column inside a csv file with Python. I can see some code on google on how to read that csv file but I don’t know how to read the xml column inside the csv file. Can …
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-xml-to-csv-in-python
Convert XML to CSV in Python - GeeksforGeeks
July 23, 2025 - To make XML data easier to process, we often convert it to a CSV file. In this article, we will explore how to convert XML to CSV step-by-step with the help of the built-in xml.etree.ElementTree module and the powerful pandas library.
🌐
Blogger
beyondvalence.blogspot.com › 2014 › 04 › python-converting-csv-to-xml-and-json.html
Valence Analytics: Python: Converting CSV to XML and JSON
February 15, 2023 - To read the CSV file, we use the csv.reader() method, and set the delimiter to a comma. Then we create the XML file to which we will write the output, and write the XML heading and the root element.
🌐
Sonra
sonra.io › home › xml › xml conversion using python in 2025
XML Conversion Using Python in 2026 - Sonra
January 14, 2025 - Python provides several libraries and methods suitable for XML conversion, catering to various data handling requirements. While XML is one of many formats used for data exchange and storage, its conversion into more commonly used formats like CSV can enhance data interoperability and integration. This overview introduces key Python libraries, including lxml, ElementTree, xmltodict, BeautifulSoup, and pandas, that facilitate the conversion process.
🌐
Stack Abuse
stackabuse.com › reading-and-writing-xml-files-in-python-with-pandas
Reading and Writing XML Files in Python with Pandas
October 14, 2023 - XML (Extensible Markup Language) is a markup language used to store structured data. The Pandas data analysis library provides functions to read/write data for most of the file types. For example, it includes read_csv() and to_csv() for interacting with CSV files.
🌐
Datasciencehorizons
datasciencehorizons.com › advanced-file-handling-in-python-working-with-csv-json-and-xml
Advanced File Handling in Python: Working with CSV, JSON, and XML – Data Science Horizons
May 17, 2024 - Use schemas to validate JSON data. XML: Manage namespaces and avoid excessive depth in element hierarchy. Use libraries that support schema validation. This article has covered the essentials of handling CSV, JSON, and XML files using Python. We explored various methods and libraries, including `csv`, `json`, `xml.etree.ElementTree`, and `pandas`, and provided practical examples for each file format.