亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Python解析XSD文件

如何使用Python解析XSD文件

繁星點點滴滴 2023-10-18 15:25:33
如何解析下面的 XSD 以獲取其中的 3 個名稱<xsd:complexType name="Register-Type" abstract="true">我想獲取名稱“measures”、“description”和“notes”,并將每個名稱放入 csv 的一列中(沒有其他信息,現在只有這 3 個名稱顯示為標題)。我正在嘗試使用 lxml,但我不知道如何進入我想要的特定復雜類型標簽。我嘗試過的from xml.etree import ElementTreeimport csvtree = ElementTree.parse('Omschema.xsd')sitescope_data = open('Out.csv', 'w', newline='', encoding='utf-8')csvwriter = csv.writer(sitescope_data)#Create all needed columns here in order and writes them to excel filedef recurse(root):    for child in root:        recurse(child)        print(child.tag)    for event in root.findall('{http://www.w3.org/2001/XMLSchema}complexType'):        event_data = []        event_id = event.find('{http://www.w3.org/2001/XMLSchema}sequence')        if event_id != None:            event_id = event_id.text        event_data.append(event_id)        csvwriter.writerow(event_data)root = tree.getroot()recurse(root)sitescope_data.close()
查看完整描述

1 回答

?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

既然您標記了 BeautifulSoup,那么具體操作方法如下:


import csv

from bs4 import BeautifulSoup


soup = BeautifulSoup(your_xml, "xml")


tag_names = soup.find("xsd:complexType", {"name": "Register-Type"})


with open('data.csv', 'w') as f:

    headers = [tag['name'] for tag in tag_names.find_all("xsd:element")]

    writer = csv.DictWriter(f, fieldnames=headers)

    writer.writeheader()

數據.csv:


measures,description,notes


查看完整回答
反對 回復 2023-10-18
  • 1 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號