简体   繁体   English

类型错误:使用带有字符串参数的 itemgetter 时,字符串索引必须是整数

[英]TypeError: string indices must be integers when using itemgetter with string argument

I am trying to achieve categorisation of an IFC model using IfcOpenShell.我正在尝试使用 IfcOpenShell 对 IFC model 进行分类。

In the first step I extract the the attributes GlobalId and ObjectType from a list of IFC model elements.在第一步中,我从 IFC model 元素列表中提取属性GlobalIdObjectType Then I would like to sort the information using the ObjectType attribute, to receive the following information from the model:然后我想使用ObjectType属性对信息进行排序,以从 model 接收以下信息:

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'}

The elements with same ObjectType and with different GlobalId should be combined in one group, to get a categorisation.具有相同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)

With above code, I get the error message Exception has occurred: TypeError string indices must be integers .使用上面的代码,我收到错误消息Exception has occurred: TypeError string indices must be integers

In the first loop, you collect the elements of rows as strings in the form '3vpWoB...: Basiswand...' .在第一个循环中,您将rows的元素收集为'3vpWoB...: Basiswand...'形式的字符串。 For example:例如:

['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'
]

Then, when you sort and group with itemgetter as key function, you would have to specify a position or range in the string.然后,当您使用itemgetter作为键 function 进行排序和分组时,您必须在字符串中指定 position 或范围。 Eg when you want to compare based on the 24th character use itemgetter(24) or likewise to compare based on the trailing substring use itemgetter(slice(24,-1)) .例如,当您想基于第 24 个字符进行比较时,使用itemgetter(24)或同样基于尾随的 substring 使用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'

If you try to use a string as index to get a substring, like in itemgetter('IfcBuildingElement') you get the error you see.如果您尝试使用字符串作为索引来获取 substring,就像在itemgetter('IfcBuildingElement')中一样,您会看到您看到的错误。

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

So in order to successfully use itemgetter('Element') as key for sorting and grouping, you want to collect the rows as dictionaries of the form {'GlobalId': '3vpWoB...', 'Element': 'Basiswand...'} instead of strings.因此,为了成功使用itemgetter('Element')作为排序和分组的键,您希望将行收集为{'GlobalId': '3vpWoB...', 'Element': 'Basiswand...'}而不是字符串。 For example:例如:

[{'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'}
]

This could be achieved with the following code in the loop to collect the rows.这可以通过循环中的以下代码来收集行来实现。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM