【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
相关推荐
belong_my_offer14 分钟前
认识到精通函数
开发语言·python
卡次卡次11 小时前
vibecoding起步注意点:插件、Skills、MCP、Hooks
服务器·数据库·python·oracle
我的xiaodoujiao2 小时前
API 接口自动化测试详细图文教程学习系列24--如何用Pytest去设计接口测试用例并执行
python·学习·测试工具·pytest
zhangfeng11332 小时前
ai 模型加密,强化版终极防盗方案 支持烧录的显卡列表
人工智能·pytorch·python
半个落月2 小时前
深入理解 Python dict 与 set:从哈希表底层到高性能实战
python
带派擂总2 小时前
Python全栈开发 Day10_用户管理系统
python
databook2 小时前
用 SymPy 解决 Manim 曲线绘制速度不均的问题
python·数学·动效
宇宙无敌程序员菜鸟2 小时前
浅玩CRUD Agent
python
程序大视界2 小时前
【Python系列课程】Python入门教程
开发语言·人工智能·python
morning_judger2 小时前
Agent系列(二)-记忆系统的设计
开发语言·python·机器学习