从SAP2000 XML截面库提取数据到Excel
问题
SAP2000的截面库是XML格式,带有命名空间,直接用Python解析会找不到元素。
解决方案
python
import xml.etree.ElementTree as ET
import pandas as pd
# XML文件路径
path = r'E:\Program Files\Computers and Structures\SAP2000 26\Property Libraries\Sections\BSShapes2006.xml'
tree = ET.parse(path)
root = tree.getroot()
# 关键:定义命名空间
ns = {'csi': 'http://www.csiberkeley.com'}
# 提取数据(以STEEL_ANGLE为例,可改成其他截面类型)
data = []
for section in root.findall('.//csi:STEEL_ANGLE', ns):
row = {}
for child in section:
tag = child.tag.replace('{http://www.csiberkeley.com}', '')
row[tag] = child.text
data.append(row)
# 导出Excel
df = pd.DataFrame(data)
df.to_excel('sections.xlsx', index=False)
常见截面类型标签
| 标签 | 截面类型 |
|---|---|
STEEL_ANGLE |
角钢 |
STEEL_I_SECTION |
工字钢 |
STEEL_CHANNEL |
槽钢 |
STEEL_TEE |
T型钢 |
STEEL_TUBE |
方管 |
STEEL_PIPE |
圆管 |
批量提取所有类型
python
import xml.etree.ElementTree as ET
import pandas as pd
path = r'你的XML路径.xml'
tree = ET.parse(path)
root = tree.getroot()
ns = {'csi': 'http://www.csiberkeley.com'}
section_types = ['STEEL_ANGLE', 'STEEL_I_SECTION', 'STEEL_CHANNEL',
'STEEL_TEE', 'STEEL_TUBE', 'STEEL_PIPE']
with pd.ExcelWriter('all_sections.xlsx') as writer:
for sec_type in section_types:
data = []
for section in root.findall(f'.//csi:{sec_type}', ns):
row = {child.tag.split('}')[1]: child.text for child in section}
data.append(row)
if data:
pd.DataFrame(data).to_excel(writer, sheet_name=sec_type, index=False)
依赖
bash
pip install pandas openpyxl