如何解析下面的 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
添加回答
举报
0/150
提交
取消