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 Top answer 1 of 5
6
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,,
2 of 5
6
Using Pandas, parsing all xml fields.
import xml.etree.ElementTree as ET
import pandas as pd
tree = ET.parse("file.xml")
root = tree.getroot()
get_range = lambda col: range(len(col))
l = [{r[i].tag:r[i].text for i in get_range(r)} for r in root]
df = pd.DataFrame.from_dict(l)
df.to_csv('file.csv')
Convert xml to excel/csv
Please help me in converting XML file into excel/csv. Thank you in advance. More on discuss.python.org
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
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
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
Videos
02:09
Convert Each CSV Row into Separate XML Files with Python - YouTube
02:25
Efficiently Generate XML from CSV Data Using lxml in Python - YouTube
python csv to xml
06:12
How to Convert CSV to XML Using Python with #tkinter and Pandas ...
Convert XML to CSV in Python | Full Source Code | Complete ...
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
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")
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
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 …