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 OverflowLike 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.
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
Python - Converting xml to csv using Python pandas - Stack Overflow
I am new in here and I have been trying to create a small python script to convert xml to csv. Based on my reading various post here in Stackoverflow I have managed to come up with a sample code that More on stackoverflow.com
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
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
Videos
08:51
How To Convert XML to CSV In Python - YouTube
06:49
How to convert an XML file to python pandas dataframe - reading ...
Convert XML to CSV in Python | Full Source Code | Complete ...
09:37
Convert an XML File to CSV with Python - Supports Nested XML - YouTube
12:42
Convert XML to CSV in Python | Python Tutorial - YouTube
04:14
Convert XML to CSV in Python | Full Source Code | Complete Tutorial ...
Syntax Byte
syntaxbytetutorials.com › home › import xml into pandas and convert to csv
Import XML into Pandas and Convert to CSV - Syntax Byte
November 13, 2024 - Use pandas to convert a nested XML file to a CSV in only three lines of Python.
PyShine
pyshine.com › XML-to-CSV-using-Pandas-in-Python
How to parse XML file and save the data as CSV | PyShine
January 3, 2021 - 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.
Quora
quora.com › How-do-you-convert-XML-to-CSV-in-Python
How to convert XML to CSV in Python - Quora
January 21, 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”, ...
Table Convert
tableconvert.com › home › convert xml to pandas dataframe online
Convert XML to Pandas DataFrame Online - Table Convert
January 11, 2019 - 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
Top answer 1 of 2
2
you have an indentation issue on
if len(elem):
I guess this should resolve it.
2 of 2
1
If I understood your question correctly, you need to traverse the XML tree, so you probably want to have a recursive function that does that. Something like the following:
import pandas as pd
import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()
def f(elem, result):
result[elem.tag] = elem.text
cs = elem.getchildren()
for c in cs:
result = f(c, result)
return result
d = f(root, {})
df = pd.DataFrame(d, index=['values']).T
df
Out:
values
Transmission \n
TransmissionBody \n
level1 \n
level2 \n
level3 \n
level4 \n
level5 \n
level6 \n
ColA ABC
ColB 123
Update: Here's when we need to do it on multiple XML files. I've added another file similar to the original one with ColA, ColB rows replaced with
<ColA>DEF</ColA>
<ColB>456</ColD>
Here's the code:
def f(elem, result):
result[elem.tag] = elem.text
cs = elem.getchildren()
for c in cs:
result = f(c, result)
return result
result = {}
for file in glob.glob('*.xml'):
tree = ET.parse(file)
root = tree.getroot()
result = f(root, result)
df = pd.DataFrame(result, index=['values']).T
df
And the output:
0 1
Transmission \n \n
TransmissionBody \n \n
level1 \n \n
level2 \n \n
level3 \n \n
level4 \n \n
level5 \n \n
level6 \n \n
ColA ABC DEF
ColB 123 456
Medium
medium.com › @meiyee715 › converting-xml-to-csv-python-xml-etree-25fec8e72626
Converting XML to CSV: Python xml.etree | by Amy Leong | Medium
October 14, 2023 - 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.