XML to csv conversion using python xml or simplified_scrapy

Viewed 186

I am trying to cover 1 XML file to CSV/xls. I have tried below code but seems not working. I have to fetch CentreFrequency for "NodeName" whose Role = Base. Below is the sample XML https://pastebin.com/Ln4Ug0hN

from bs4 import BeautifulSoup
import pandas as pd
import lxml
import xml.etree.cElementTree
import openpyxl
import inspect
import os
import tkinter
from tkinter import filedialog

root = tkinter.Tk()
dirname = filedialog.askopenfilename(parent=root, initialdir="/",
                                    title='Please select a file')




root = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

sites = "xmll.xml"

with open(sites, "r",encoding='unicode_escape') as f:
    xml_data = f.read()

soup = BeautifulSoup(xml_data, "xml")
#ss=soup.find_all("NodeName").text
# 'INAPKVLIVGLRTW6001ENBIB5004'


tag1 = input("Enter tagname1:")
tag2 = input("Enter tagname2:")
# in a loop

data = []
dd = []

for td in soup.find_all(tag1):
    data.append({"Name": td.text})
    
for snmp in soup.find_all(tag2):
    dd.append({"CentreFrequency": snmp.text})

df1 = pd.DataFrame(data)
df2 = pd.DataFrame(dd)
    
df = pd.merge(df1,df2,left_index=True, right_index=True)
 
1 Answers

I found a very easy library to usefor XML to CVS from one of the user named "simplified_scrapy" Below is the solution you can try

from simplified_scrapy import SimplifiedDoc, utils, req
import inspect
import time
import os
import pandas as pd

def delEmptyRows(name, encoding="utf-8"):
    lines = utils.getFileLines(name, encoding=encoding)
    lines = [line for line in lines if line.strip()!='']
    utils.saveFile(name, "".join(lines), encoding=encoding)

root = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

xml = utils.getFileContent(root +'/xml.xml')
#xml = open('mumbai2.xml','r').read
doc = SimplifiedDoc(xml)
lstNodeResult = doc.selects('IBridge2ConfigResult')
data = [['NodeName','CenterFrequency']]

for result in lstNodeResult:
    lstrole = result.selects('IBridge2Config>Role')

    frequency =  result.CenterFrequency.text       
    NodeName = result.NodeName.text
    row = [NodeName,frequency]

    if NodeName !=' ':    
        data.append(row)
    # print (data)
    utils.save2csv('Frequency_List.csv',data)
    delEmptyRows('Frequency_List.csv')



Related