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
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-xml-to-csv-in-python
Convert XML to CSV in Python - GeeksforGeeks
July 23, 2025 - Store it into a pandas DataFrame. Export the DataFrame to a CSV file. To download the XML data used in the examples, click here. Python Program to Convert XML to CSV.
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
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
September 8, 2024
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.
🌐
Like Geeks
likegeeks.com › home › python › pandas › export xml to csv using python pandas
Export XML to CSV using Python Pandas
December 16, 2023 - The following examples will provide ... into CSV files using Pandas. We’ll use Pandas read_xml() to read the XML content and Pandas to_csv() to export the data to CSV format....
🌐
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.
🌐
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
April 26, 2024 - 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.
🌐
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   July 22, 2022
Find elsewhere
🌐
YouTube
youtube.com › hello code
Convert XML to CSV in Python | Python Tutorial - YouTube
Check out https://www.hellocodeclub.com for more tutorials and projectsLearn how to convert XML to CSV using PythonContent00:00 - Introduction00:59 - Explain...
Published   April 9, 2022
Views   7K
🌐
Syntax Byte
syntaxbytetutorials.com › home › import xml into pandas and convert to csv
Import XML into Pandas and Convert to CSV - Syntax Byte
December 13, 2023 - import pandas as pd df = pd.read_xml('samplefile.xml', stylesheet='samplefilestylesheet.xslt', xpath='//user') df.to_csv('xml_to_csv.csv', index=False)
🌐
PyShine
pyshine.com › XML-to-CSV-using-Pandas-in-Python
How to parse XML file and save the data as CSV | PyShine
December 12, 2023 - 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")
🌐
Delft Stack
delftstack.com › home › howto › python › xml to csv python
How to Convert XML to CSV Using Python | Delft Stack
February 7, 2026 - 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:
🌐
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)
🌐
Quora
quora.com › How-do-you-convert-XML-to-CSV-in-Python
How to convert XML to CSV in Python - Quora
October 8, 2025 - 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
August 21, 2024 - 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.
🌐
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.
🌐
Table Convert
tableconvert.com › home › convert xml to pandas dataframe online
Convert XML to Pandas DataFrame Online - Table Convert
January 6, 2026 - 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
🌐
Thomas Suedbroecker
suedbroecker.net › 2023 › 03 › 08 › how-to-convert-xml-data-to-csv-in-a-jupyter-notebook-with-python
How to convert XML data to CSV in a Jupyter Notebook with Python? – Thomas Suedbroecker's Blog
March 1, 2024 - Below you can see an example of a table for the XML to CSV extraction, created with an open-source data analysis and manipulation tool for Python called pandas.
🌐
GitHub
gist.github.com › hunterowens › 92d9286461979fd400f6
XML->CSV · GitHub
November 29, 2014 - Save hunterowens/92d9286461979fd400f6 to your computer and use it in GitHub Desktop. Download ZIP · XML->CSV · Raw · xml2csv.py · This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Stack Abuse
stackabuse.com › reading-and-writing-xml-files-in-python-with-pandas
Reading and Writing XML Files in Python with Pandas
February 17, 2022 - 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.org
discuss.python.org › python help
Read xml column inside csv file with Python - Python Help - Discussions on Python.org
September 8, 2024 - 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 …