抱歉,是否已经问过这个问题。
我在下面的XML文件中想要将其转换为CSV或Excel。
在这里,我想提取IpRoutelist下的NodeName及其子DestIPAddress。
<?xml version="1.0" encoding="utf-8"?>
OK
OK
IMUMB0899
IMUMB0899
1000 SFP
TT
DND
DND
0
Enabled
Macro
397063
120
172.172.6.20
255.255.255.255
172.21.200.1
2405:20:1::
40
2405:20:101:4:7:2:61:1
我尝试下面的代码提取名称和IProute,但是当我尝试合并tham时,只有一个IP路由针对NodeName,但是有两个可用。
from bs4 import BeautifulSoup
import pandas as pd
import lxml
import xml.etree.cElementTree
import openpyxl
import inspect
import os
sites = "xml"
with open(sites, "r",encoding='unicode_escape') as f:
xml_data = f.read()
soup = BeautifulSoup(xml_data, "xml")
tag1 = input("Enter tagname1:")
tag2 = input("Enter tagname2:")
data = []
dd = []
for td in soup.find_all(tag1):
data.append({"NodeName": td.text})
for iproute in soup.find_all(tag2):
dd.append({"IpRoute": iproute.text})
df1 = pd.DataFrame(data)
df2 = pd.DataFrame(dd)
df = pd.merge(df1,df2,left_index=True, right_index=True)
df.to_excel(sites + '.xlsx', sheet_name='Detail', index = False)
print("*************Done*************")
Expected Output: