【问题标题】:Python - using regex to search for a string then append each match to a new listPython - 使用正则表达式搜索字符串,然后将每个匹配项附加到新列表
【发布时间】:2018-05-26 17:06:48
【问题描述】:

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

#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 ) ;

以下是我目前使用的方法:

faces_txt = re.findall(r'#(\d+) = ADVANCED_FACE.*;', text)

    faces = [int(face) for face in faces_txt]
    print('Face IDs = ', faces)

哪些输出:

Face IDs =  [12, 73, 99, 131, 181, 214, 244, 273, 330, 358]

如果我想为“ADVANCED_FACE”的每个匹配项创建一个列表(按顺序命名,如“Face1、Face2、Face3...”)而不是追加所有这些值到同一个列表?

【问题讨论】:

    标签: python cad step


    【解决方案1】:

    如果没有关于您想要的确切输出的更多详细信息,也可能没有更多输入,听起来您想要一个列表列表或列表字典。

    faces_txt = re.findall(r'#(\d+) = ADVANCED_FACE.*;', text)
    faces = [int(face) for face in faces_txt]
    
    print('Face IDs = ', faces)
    
    LoL = []
    DoL = {}
    for i, faceId in enumerate(faces):
        LoL.append([faceId])
        DoL.update({"Face{}".format(i): [faceId]})
    
    print(LoL)
    print(DoL)
    
    print(DoL['Face0'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 2012-05-28
      • 2012-05-23
      相关资源
      最近更新 更多