【问题标题】:TypeError: string indices must be integers when using itemgetter with string argument类型错误:使用带有字符串参数的 itemgetter 时,字符串索引必须是整数
【发布时间】:2020-11-26 09:42:44
【问题描述】:

我正在尝试使用 IfcOpenShell 对 IFC 模型进行分类。

在第一步中,我从 IFC 模型元素列表中提取属性 GlobalIdObjectType。然后我想使用ObjectType属性对信息进行排序,以接收来自模型的以下信息:

Basiswand:Bestand 08.0:161894
    {'GlobalId': '3vpWoB_K1EZ8RCaYmNGs6M', 'Element': 'Basiswand:Bestand 08.0:161894'}
    {'GlobalId': '3vpWoB_K1EZ8RCaYmNGsB2', 'Element': 'Basiswand:Bestand 08.0:161894'}
Fenster 1-flg - Variabel
    {'GlobalId': '3vpWoB_K1EZ8RCaYmNGssv', 'Element': 'Fenster 1-flg - Variabel'}
    {'GlobalId': '3vpWoB_K1EZ8RCaYmNGsqI', 'Element': 'Fenster 1-flg - Variabel'}

ObjectType 相同,GlobalId 不同的元素应合并为一组,进行分类。

rows =[]   
buildingelement = model.by_type('IfcBuildingElement')
for buildingelement in model.by_type('IfcBuildingElement'):
    rows.append(str(buildingelement.GlobalId) + ': ' + str(buildingelement.ObjectType))

print(rows)


from operator import itemgetter 
from itertools import groupby
    # Sort by the desired field first
rows.sort(key=itemgetter('IfcBuildingElement'))
    # Iterate in groups
for date, items in groupby(rows, key=itemgetter('IfcBuildingElement')): 
    print(date)
    for i in items: 
        print(' ', i)

使用上面的代码,我收到错误消息Exception has occurred: TypeError string indices must be integers

【问题讨论】:

    标签: python sorting ifc


    【解决方案1】:

    在第一个循环中,您将rows 的元素收集为'3vpWoB...: Basiswand...' 形式的字符串。例如:

    ['3vpWoB_K1EZ8RCaYmNGs6M: Basiswand:Bestand 08.0:161894',
     '3vpWoB_K1EZ8RCaYmNGsB2: Basiswand:Bestand 08.0:161894',
     '3vpWoB_K1EZ8RCaYmNGssv: Fenster 1-flg - Variabel',
     '3vpWoB_K1EZ8RCaYmNGsqI: Fenster 1-flg - Variabel'
    ]
    

    然后,当您使用itemgetter 作为键函数进行排序和分组时,您必须指定字符串中的位置或范围。例如。如果要根据第 24 个字符进行比较,请使用 itemgetter(24),或者同样根据尾随子字符串进行比较,请使用 itemgetter(slice(24,-1))

    >>> '3vpWoB_K1EZ8RCaYmNGs6M: Basiswand:Bestand 08.0:161894'[24]
    'B'
    >>> '3vpWoB_K1EZ8RCaYmNGs6M: Basiswand:Bestand 08.0:161894'[24:-1]
    'Basiswand:Bestand 08.0:161894'
    

    如果您尝试使用字符串作为索引来获取子字符串,例如在itemgetter('IfcBuildingElement') 中,您会收到您看到的错误。

    >>> '3vpWoB_K1EZ8RCaYmNGs6M: Basiswand:Bestand 08.0:161894'['IfcBuildingElement']
    TypeError: string indices must be integers 
    

    因此,为了成功使用itemgetter('Element') 作为排序和分组的键,您希望将行收集为表单的字典 {'GlobalId': '3vpWoB...', 'Element': 'Basiswand...'} 而不是字符串。例如:

    [{'GlobalId':'3vpWoB_K1EZ8RCaYmNGs6M', 'Element':'Basiswand:Bestand 08.0:161894'},
     {'GlobalId':'3vpWoB_K1EZ8RCaYmNGsB2', 'Element':'Basiswand:Bestand 08.0:161894'},
     {'GlobalId':'3vpWoB_K1EZ8RCaYmNGssv', 'Element':'Fenster 1-flg - Variabel'},
     {'GlobalId':'3vpWoB_K1EZ8RCaYmNGsqI', 'Element':'Fenster 1-flg - Variabel'}
    ]
    

    这可以通过在循环中使用以下代码来收集行来实现。

    rows.append({'GlobalId': buildingelement.GlobalId, 'Element': buildingelement.ObjectType})
    

    【讨论】:

      猜你喜欢
      • 2023-02-03
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 2014-12-30
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多