Using pandas and BeautifulSoup you can achieve your expected output easily:

#Code:

import pandas as pd
import itertools
from bs4 import BeautifulSoup as b
with open("file.xml", "r") as f: # opening xml file
    content = f.read()

soup = b(content, "lxml")
pkgeid =  [ values.text for values in soup.findAll("pkgeid")]
pkgname = [ values.text for values in soup.findAll("pkgname")]
time =  [ values.text for values in soup.findAll("time")]
oper =  [ values.text for values in soup.findAll("oper")]
# For python-3.x use `zip_longest` method
# For python-2.x use 'izip_longest method
data = [item for item in itertools.zip_longest(time, oper, pkgeid, pkgname)] 
df  = pd.DataFrame(data=data)
df.to_csv("sample.csv",index=False, header=None)

#output in `sample.csv` file will be as follows:
2015-09-16T04:13:20Z,Create_Product,10,BBCWRL
2015-09-16T04:13:20Z,Create_Product,18,CNNINT
2018-04-01T03:30:28Z,Deactivate_Dhct,,
Answer from Rachit kapadia on Stack Overflow
🌐
Like Geeks
likegeeks.com › home › python › pandas › export xml to csv using python pandas
Export XML to CSV using Python Pandas
December 16, 2023 - We’ll use Pandas read_xml() to read the XML content and Pandas to_csv() to export the data to CSV format. ... This example deals with a straightforward XML file where data is organized in a simple hierarchical structure.
Discussions

Convert xml to excel/csv
Please help me in converting XML file into excel/csv. Thank you in advance. More on discuss.python.org
🌐 discuss.python.org
0
0
October 15, 2022
python - Read XML using pandas parsing to csv - Stack Overflow
I have the following code to extract data from XML to CSV file, but there is an error and I don't know how to solve it. if anyone can help, please. url = "http://90.161.233.78:65519/services/u... More on stackoverflow.com
🌐 stackoverflow.com
1 month ago
ElementTree and deeply nested XML
Subreddit for posting questions and asking for general advice about all topics related to learning python. ... Does anyone know how I can parse the data in the example XML below by grabbing data from the tag to the tag? More on reddit.com
🌐 r/learnpython
1
3
July 28, 2016
How do I convert XML into a CSV in the most efficient way possible?
How To Parse and Convert XML to CSV using Python . More on reddit.com
🌐 r/datascience
7
3
July 5, 2017
🌐
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 - With this script, you can easily convert any complex XML file into a Pandas DataFrame or CSV file. This will make your data easier to work with and allow you to leverage the powerful data analysis capabilities of Python and Pandas.
🌐
Delft Stack
delftstack.com › home › howto › python › xml to csv python
How to Convert XML to CSV Using Python | Delft Stack
February 2, 2024 - We will use the same XML file as above and will parse this XML data, convert it to a dictionary using xmltodict, transform it into a pandas DataFrame, and finally write it to a CSV file. Here is the Python code that performs the conversion:
🌐
Medium
medium.com › analytics-vidhya › converting-xml-data-to-csv-format-using-python-3ea09fa18d38
Converting XML data to CSV format using Python | by Pralhad Teggi | Analytics Vidhya | Medium
November 20, 2019 - Once the extraction is completed, the list is written to the csv file as below. Also close the file descriptor. 6. Lets import pandas library and read the csv file.
🌐
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.
Find elsewhere
🌐
YouTube
youtube.com › watch
Convert an XML File to CSV with Python - Supports Nested XML - YouTube
In this video, I show you how to use Python and pandas to convert an XML file to CSV. Nested XML is also supported by using a stylesheet to adjust the file t...
Published   March 5, 2025
🌐
PyShine
pyshine.com › XML-to-CSV-using-Pandas-in-Python
How to parse XML file and save the data as CSV | PyShine
November 13, 2024 - So all we need to provide the name of the xml file in the Python script below: ... import pandas as pd import xml.etree.ElementTree as ET filename = 'songs' tree = ET.parse(filename+'.xml') root = tree.getroot() element_dict = {} for elem in root.iter(): element_dict[elem.tag]=[] for elem in root.iter(): if elem.text=='\n': element_dict[elem.tag].append(elem.attrib) else: element_dict[elem.tag].append(elem.text) def make_list(dict_list, placeholder): lmax = 0 for lname in dict_list.keys(): lmax = max(lmax, len(dict_list[lname])) for lname in dict_list.keys(): ll = len(dict_list[lname]) if ll < lmax: dict_list[lname] += [placeholder] * (lmax - ll) return dict_list ans = make_list(element_dict,-1) df = pd.DataFrame(ans) print(df) df.to_csv(filename+".csv")
🌐
Python.org
discuss.python.org › python help
Convert xml to excel/csv - Python Help - Discussions on Python.org
October 15, 2022 - Please help me in converting XML file into excel/csv. Thank you in advance.
🌐
Plain English
plainenglish.io › home › blog › python › convert xml to csv using python
Convert XML to CSV Using Python
February 17, 2022 - #2 # Reading xml file with open("sample.xml", 'r') as file: filedata = file.read() # Converting xml to python dictionary (ordered dict) data_dict = xmltodict.parse(filedata)
🌐
Stack Abuse
stackabuse.com › reading-and-writing-xml-files-in-python-with-pandas
Reading and Writing XML Files in Python with Pandas
August 21, 2024 - 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.
🌐
Python Pool
pythonpool.com › home › blog › xml to csv conversion using python
XML to CSV Conversion Using Python - Python Pool
June 14, 2021 - Hello coders!! In this article we will learn how to convert an xml file into a csv file in python. So without much ado lets directly dive into the topic.
🌐
Quora
quora.com › How-do-you-convert-XML-to-CSV-in-Python
How to convert XML to CSV in Python - Quora
October 14, 2023 - Answer (1 of 4): In a strict sense? You don’t. CSV is a format (if it can even be called that!) for encoding row-based data. XML is a format for encoding tree-based data. One expects all entries to follow a simple, “all of these entries have the same fields, and a value in those fields”, ...
🌐
Medium
medium.com › @meiyee715 › converting-xml-to-csv-python-xml-etree-25fec8e72626
Converting XML to CSV: Python xml.etree | by Amy Leong | Medium
April 11, 2025 - Replace path_to_your_xml_file.xml and path_to_output.csv with your desired paths. The provided script is a basic example, and real-world XML files can vary widely in their structure. Depending on the nature of the XML, you may need to account for attributes, nested elements, and other complexities. The beauty of Python is that it offers the flexibility to handle these scenarios with a bit more logic.
🌐
Table Convert
tableconvert.com › home › convert xml to pandas dataframe online
Convert XML to Pandas DataFrame Online - Table Convert
May 11, 2025 - Use the extension to detect and extract tables from any page, then paste the data here to convert XML to PandasDataFrame. Instantly extract tables from any webpage without copy-pasting - professional data extraction made simple · Convert extracted tables to Excel, CSV, JSON, Markdown, SQL, and more with our advanced table converter
🌐
Medium
medium.com › @ayushnandanwar003 › simplifying-data-conversion-a-comprehensive-guide-to-converting-xml-to-csv-in-python-b22c24b02628
Simplifying Data Conversion: A Comprehensive Guide to Converting XML to CSV in Python | by Ayush Nandanwar | Medium
April 26, 2024 - To convert XML data to CSV format in Python, we’ll leverage the power of two core modules: xml.etree.ElementTree for parsing XML and csv for handling CSV files.
🌐
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() function. The file always ends with its extension, in the case of CSV is ‘.csv’, and in the case of XML is ‘.xml’. The endswith() function checks the last characters of the string with the given input.