【Python】从SAP2000 XML截面库提取数据到Excel

从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
相关推荐
华研前沿标杆游学11 小时前
企业参访入口 | 走进华为坂田基地,深圳华为坂田基地考察游学参访
python
朝朝辞暮i11 小时前
VLA 系统学习第 12 课:为什么机器人不能只看一帧?——时间序列、Observation History 与 Action Chunk
人工智能·python·深度学习·神经网络·vla
朝朝辞暮i11 小时前
VLA 系统学习第 8 课:Optimizer 到底在干什么?——从 Learning Rate 到 SGD、Momentum 和 Adam
人工智能·python·深度学习·神经网络·vla
镜子AI11 小时前
教培机构多模态内容跨模态检索系统:基于CLIP的统一表征与图文混合召回算法
人工智能·python·算法·机器学习
落魄大学生之流水线上谋生计11 小时前
Selenium 入门到实战:用 Python 写出稳定的浏览器自动化
python·selenium·自动化
汤米粥11 小时前
后端开发主流技术方案
java·python·golang·php·nodejs·后端开发
我的xiaodoujiao11 小时前
Django 基础知识详细图文教程 13-Django 模型定义与使用 3
数据库·后端·python·测试工具·oracle·django
小静AI工程实验室12 小时前
robots.txt 不是门禁:RFC 9309 规则、缓存与 Python 爬虫的 10 个边界
爬虫·python·缓存
沉下心来学鲁班12 小时前
DeepAgents 记忆机制:让 AI Agent 拥有“跨对话“的记忆
服务器·人工智能·python