Python - 将新的键:值对添加到字典中的字典中

时间:2017-12-13 12:48:59

标签: python dictionary cad step

我目前正在尝试从具有这种格式的文件中提取一些数据:

#12 = ADVANCED_FACE ( 'NONE', ( #194 ), #326, .F. ) ;
...
#159 = EDGE_LOOP ( 'NONE', ( #21, #124, #264, #145 ) ) ;
...
#194 = FACE_OUTER_BOUND ( 'NONE', #159, .T. ) ;
...
#326 = PLANE ( 'NONE',  #352 ) ;

我使用以下代码搜索ADVANCED_FACE并将它们添加到字典中,如下所示:

    DoL = {}

    #search for advanced faces and extract IDs
    faces_txt = re.findall(r'#(\d+) = ADVANCED_FACE.*;', text)
    faces = [int(face) for face in faces_txt]

    #append advanced face IDs to dict
    for i, faceId in enumerate(faces):
        DoL.update({"Face{}".format(i): {'AdvancedFace': [faceId]}})

这会找到所有ADVANCED_FACES并创建如下输出:

Face0 = {'AdvancedFace': [12]}
Face1 = {'AdvancedFace': [73]}
Face2 = {'AdvancedFace': [99]}
Face3 = {'AdvancedFace': [131]}
Face4 = {'AdvancedFace': [181]}
Face5 = {'AdvancedFace': [214]}
Face6 = {'AdvancedFace': [244]}
Face7 = {'AdvancedFace': [273]}
Face8 = {'AdvancedFace': [330]}
Face9 = {'AdvancedFace': [358]}

现在我需要找到所有的FACE_OUTER_BOUND并以类似的方式将它们添加到字典中,以便输出看起来像:

Face0 = {'AdvancedFace': [12], 'FaceOuterBound': [194]}
Face1 = {'AdvancedFace': [73], 'FaceOuterBound': [53]}
...

我尝试使用以下方法,但它会抛出一个错误,上面写着' dict'没有属性'追加'。

#extract face outer bound IDs from advanced face strings
fob_txt = re.findall(r'ADVANCED_FACE.{13}#(\d+).*;', text)
fobs = [int(fob) for fob in fob_txt]

#append face outer bound IDs to dict
for i, fobId in enumerate(fobs):
    DoL["Face{}".format(i)].append({'FaceOuterBounds': (fobId)})

1 个答案:

答案 0 :(得分:0)

为什么不这样做? DoL [“Face {}”。format(i)]。update({'FaceOuterBounds':[fobId]})

也许我想念这个问题......