将 XML 转换/解析为 CSV 文件

问题描述 投票:0回答:1

这里有人可以指导我如何使用 python 3.9 将此 xml 转换为 csv 吗?目前我很难解析这个 xml。

下面是我的xml结构:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE raml SYSTEM 'raml20.dtd'>
<raml version="2.0" xmlns="raml20.xsd">
  <cmData type="actual">
    <header>
      <log dateTime="2023-09-11T09:32:44.000+08:00" action="created" appInfo="ActualExporter">UIValues are used</log>
    </header>
    <managedObject class="LNCEL" version="FLF22R3_2207_10_2207_10" distName="PLMN-PLMN/MRBTS-10000/LNBTS-100007/LNCEL-10" id="10000">
      <p name="mcc">100</p>
      <p name="mnc">20</p>
      <p name="name">Cell01</p>
      <p name="a1TimeToTriggerDeactInterMeas">320ms</p>
      <p name="a2RedirectQci1">disabled</p>
    </managedObject>
  </cmData>
</raml>

我想要这样的输出:https://i.stack.imgur.com/jK3hd.png

先谢谢大家,期待您的意见。

python-3.x xml-parsing
1个回答
0
投票

这是一个如何使用 解析 XML 的示例:

import pandas as pd
from bs4 import BeautifulSoup

with open("your_file.xml", "r") as f_in:
    soup = BeautifulSoup(f_in.read(), "xml")

header = soup.header
dt = header.log["dateTime"]

all_data = []
for mo in soup.select("managedObject"):
    version = mo["version"]
    dist_name = mo["distName"]
    moid = mo["id"]
    all_data.append(
        {
            "DATETIME": dt,
            "VERSION": version,
            "DISTNAME": dist_name,
            "MOID": moid,
            **{p["name"]: p.text for p in mo.select("p") for a in p.attrs},
        }
    )

df = pd.DataFrame(all_data)
print(df)

打印:

                        DATETIME                  VERSION                                     DISTNAME   MOID  mcc mnc    name a1TimeToTriggerDeactInterMeas a2RedirectQci1
0  2023-09-11T09:32:44.000+08:00  FLF22R3_2207_10_2207_10  PLMN-PLMN/MRBTS-10000/LNBTS-100007/LNCEL-10  10000  100  20  Cell01                         320ms       disabled
© www.soinside.com 2019 - 2024. All rights reserved.