【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
相关推荐
常常有1 小时前
中间件与依赖系统:构建高效 Web 后端的双重利器
开发语言·python·中间件·fastapi
Zephyr_01 小时前
python基础
python
Royzst1 小时前
一、集合概述(前置基础)
开发语言·windows·python
平安的平安1 小时前
Python大模型Function Calling实战:让AI拥有工具使用能力
开发语言·人工智能·python
源码之家1 小时前
计算机毕业设计:Python中药材数据可视化与智能分析平台 Django框架 中药数据分析 医药数据分析数据分析 可视化 爬虫 (建议收藏)✅
python·深度学习·信息可视化·数据分析·django·课程设计
q_35488851531 小时前
计算机毕业设计:Python中药材天地网数据挖掘与可视化系统 Django框架 中药数据分析 医药数据分析数据分析 可视化 爬虫 (建议收藏)✅
python·数据挖掘·数据分析·django·flask·课程设计
醉舞经阁半卷书11 小时前
LangGraph详解
开发语言·人工智能·python·深度学习·机器学习·自然语言处理
抹茶咖啡2 小时前
IT运维的365天--045 WPS突然就不能正常打开Excel文件了
excel·it运维·wps
测试员周周2 小时前
【AI测试功能6】功能测试的自动化率:哪些该自动、哪些必须人工——AI测试人机协作决策指南
开发语言·人工智能·python·功能测试·单元测试·自动化·测试用例