第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用Python解析XSD文件

如何使用Python解析XSD文件

如何解析下面的 XSD 以獲取其中的 3 個(gè)名稱<xsd:complexType name="Register-Type" abstract="true">我想獲取名稱“measures”、“description”和“notes”,并將每個(gè)名稱放入 csv 的一列中(沒有其他信息,現(xiàn)在只有這 3 個(gè)名稱顯示為標(biāo)題)。我正在嘗試使用 lxml,但我不知道如何進(jìn)入我想要的特定復(fù)雜類型標(biāo)簽。我嘗試過的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貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊

既然您標(biāo)記了 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()

數(shù)據(jù).csv:


measures,description,notes


查看完整回答
反對(duì) 回復(fù) 2023-10-18
  • 1 回答
  • 0 關(guān)注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)