You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用list.append()报TypeError无关键字参数错误求助

问题背景

我是Python初学者,当前正在开展提取建筑全部构件的项目,编写代码过程中遇到报错。

初始导入与文件读取代码

import ifcopenshell.geom
import ifcopenshell.util
from ifcopenshell.util.selector import Selector
selector = Selector()
import ifcopenshell.util.element as util
import pandas as pd
import ifcopenshell
ifc = ifcopenshell.open('FHZGR_PONTE DELL`ACCADEMIA_PH01_BEM.ifc')
ifc

构件筛选代码

elements = selector.parse(ifc, '.IfcWall | .IfcSlab | .IfcWindow | .IfcDoor | .IfcRamp | .IfcColumn | .IfcBeam | .IfcStair')
elements

存在问题的遍历转表代码

l = []
for element in elements:
    l.append(elements, columns = ['Klasse', 'Typ', 'Objekt', 'ID'])
l
df = pd.DataFrame(l)
df

运行报错信息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [17], in <cell line: 2>()
      1 l = []
      2 for element in elements:
----> 3     l.append(elements, columns = ['Klasse', 'Typ', 'Objekt', 'ID'])
      4 l
      5 df = pd.DataFrame(l)

TypeError: append() takes no keyword arguments
报错原因
  • 语法错误:Python原生列表的append()方法仅支持传入1个待追加的位置参数,不支持columns这类关键字参数,直接触发类型错误。
  • 逻辑错误:循环遍历的目标是单个构件element,代码中错误将整个构件列表elements作为追加对象传入,完全没有提取每个构件对应Klasse、Typ、Objekt、ID四个字段的实际内容。
修正后可运行代码
import ifcopenshell.geom
import ifcopenshell.util
from ifcopenshell.util.selector import Selector
selector = Selector()
import ifcopenshell.util.element as util
import pandas as pd
import ifcopenshell

ifc = ifcopenshell.open('FHZGR_PONTE DELL`ACCADEMIA_PH01_BEM.ifc')
elements = selector.parse(ifc, '.IfcWall | .IfcSlab | .IfcWindow | .IfcDoor | .IfcRamp | .IfcColumn | .IfcBeam | .IfcStair')

data_list = []
for element in elements:
    # 逐构件提取目标字段,增加空值兜底避免无类型构件触发报错
    element_info = {
        'Klasse': element.is_a(),
        'Typ': util.get_type(element).Name if util.get_type(element) else None,
        'Objekt': element.Name,
        'ID': element.GlobalId
    }
    data_list.append(element_info)

# 字典列表转DataFrame时自动匹配列名
df = pd.DataFrame(data_list)
df
修正说明
  • 移除append()方法中错误传入的关键字参数,每次循环仅向列表追加单条构件的信息字典
  • 修正循环取值逻辑,针对每个构件提取对应四个字段的真实属性值,不再重复追加整个构件列表
  • 对构件类型字段增加空值判断,规避部分未关联类型的构件触发属性不存在的运行错误
  • 列名在字典键中提前定义,生成Pandas DataFrame时会自动识别为表头,无需在列表追加阶段设置

内容的提问来源于stack exchange,提问作者Archie1969

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 08:54:19