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 - Parse the XML file. Extract the relevant data. Store it into a pandas DataFrame. Export the DataFrame to a CSV file.
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 - How to convert an XML file to nice pandas dataframe? - Stack Overflow
You can easily use xml (from the Python standard library) to convert to a pandas.DataFrame. More on stackoverflow.com
🌐 stackoverflow.com
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
Tried to use pandas to read csv files but FileNotFoundError
Relative files paths are relatively to your current working directory, os.getcwd() Try an absolute path: from pathlib import Path import pandas as pd input_file = Path(r'C:\Users\DELL\Downloads\EVACANTUSQ176N.csv') df = pd.read_csv(input_file) More on reddit.com
🌐 r/learnpython
3
3
September 17, 2024
🌐
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.
🌐
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.
🌐
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 - By understanding these common errors and implementing appropriate safeguards, you can enhance the robustness and reliability of your XML to DataFrame conversion process. With this script, you can easily convert any complex XML file into a Pandas DataFrame or CSV file.
🌐
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.
🌐
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)
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › python › xml to csv python
How to Convert XML to CSV Using Python | Delft Stack
November 13, 2024 - ... Finally, we use the to_csv() method of the DataFrame object to write the data to a CSV file named ‘students.csv’. The index=False parameter ensures that the index is not written into 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   October 11, 2010
🌐
PyShine
pyshine.com › XML-to-CSV-using-Pandas-in-Python
How to parse XML file and save the data as CSV | PyShine
January 22, 2014 - 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")
🌐
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.
🌐
Table Convert
tableconvert.com › home › convert xml to pandas dataframe online
Convert XML to Pandas DataFrame Online - Table Convert
December 9, 2022 - 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
🌐
Plain English
plainenglish.io › home › blog › python › convert xml to csv using python
Convert XML to CSV Using Python
February 17, 2022 - #3 # Selecting headers for CSV HEADERS = ['name', 'role' ,'age'] rows = [] # Interating through each element to get row data for employee in employee_data_list: name = employee["name"] role= employee["role"] age = employee["age"] # Adding data of each employee to row list rows.append([name,role,age]) #Writing to CSV with open('employee_data.csv', 'w',newline="") as f: write = csv.writer(f) write.writerow(HEADERS) write.writerows(rows)
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_xml.html
pandas.read_xml — pandas documentation - PyData |
Read XML document into a DataFrame object. ... String path, path object (implementing os.PathLike[str]), or file-like object implementing a read() function. The string can be a path. The string can further be a URL. Valid URL schemes include http, ftp, s3, and file. ... The XPath to parse required set of nodes for migration to DataFrame.``XPath`` should return a collection of elements and not a single element.
🌐
Teleport
goteleport.com › resources › tools › xml-to-csv-converter
XML to CSV Converter | Instantly Transform XML to CSV | Teleport
January 11, 2019 - As you can see, manually converting XML to CSV can quickly become challenging and prone to mistakes. Let's take a look at an automated way to simplify this process and guarantee greater accuracy in your data handling. Here’s a straightforward Python script demonstrating how to automate the conversion process: import pandas as pd import xml.etree.ElementTree as ET # Sample XML string xml_data = """<data> <record> <name>John Doe</name> <age>30</age> <city>New York</city> </record> <record> <name>Jane Smith</name> <age>25</age> <city>Los Angeles</city> </record> </data>""" # Parse XML data root
🌐
AskPython
askpython.com › home › converting data in csv to xml in python
Converting Data in CSV to XML in Python - AskPython
April 4, 2023 - This XML file can also be opened in Browser. It will produce an output like this: ... Pandas also provides a function to the Dataframe which allows the user to directly convert the CSV file to an XML file.
🌐
Oracle
blog.toadworld.com › home › python for data science – importing xml to pandas dataframe
Python for Data Science – Importing XML to Pandas DataFrame - The Quest Blog
November 13, 2025 - In my previous post, I showed how easy to import data from CSV, JSON, Excel files using Pandas package. Another popular format to exchange data is XML. Unfortunately Pandas package does not have a fun...