【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
相关推荐
大鱼>20 小时前
DSPy:LLM程序自动编译与提示词优化
开发语言·人工智能·python·深度学习
2401_8432537020 小时前
金融智能:AI如何重构银行业未来
人工智能·python·金融
uncle_ll20 小时前
服务器选型、微调范式、训练优化与环境搭建
服务器·python·gpt·llm·nlp
久久学姐20 小时前
Python开发爬虫的常用技术架构
爬虫·python·http·框架·数据存储
circuitsosk20 小时前
大规模离线数据管道构建:样本获取、清洗、加工与合成
人工智能·python·机器学习·搜索引擎
tang7778921 小时前
分布式爬虫优化指南:如何用代理IP把采集效率提升300%
分布式·爬虫·python·tcp/ip·分布式爬虫·爬虫代理·代理ip
gwf21621 小时前
Soft-RoCE与Soft-iWARP深度解析:无硬件RDMA学习环境搭建(零基础必知必会)
人工智能·python·tcp/ip·tcp·tcpdump
AI行业学习21 小时前
Claude Code + cc-switch + Git + Node.js 一站式完整安装配置教程【8.3】
git·python·安全·前端框架·node.js·html·notepad++
AI行业学习21 小时前
Claude Code + cc-switch + Git + Node.js 一站式完整安装配置教程(2026最新·国内可用版)
人工智能·git·python·安全·node.js·html·notepad++
denggun1234521 小时前
大大模型 如何通过 本地排雷 远端GPU
python·embedding·pip