【发布时间】:2018-05-27 15:09:04
【问题描述】:
好的,我正在从我创建的字典 (DoL) 中读取一个变量:
for i in DoL.keys():
fobId = DoL["{}".format(i)]["FaceOuterBound"]
可以说,在 DoL 的第一次迭代中:
fobId = 194
我试图创建一个包含变量 fobId 的正则表达式 findall:
edgeloop_txt = re.findall(r'^#'+str(fobId)+r'.*#(\d+).*;', text)
为了找到text中以#fobId开头的行:
#194 = FACE_OUTER_BOUND ( 'NONE', #159, .T. ) ;
最后提取数字#159
我的输出 print(edgeloop_txt) 只是给了我空列表:
[]
[]
[]
...
编辑(提供 MCVE):
示例文本:
...
#190 = DIRECTION ( 'NONE', ( -1.000000000000000000, -0.0000000000000000000, -0.0000000000000000000 ) ) ;
#191 = DATE_AND_TIME ( #277, #349 ) ;
#192 = ORIENTED_EDGE ( 'NONE', *, *, #253, .T. ) ;
#193 = CARTESIAN_POINT ( 'NONE', ( 75.00000000000000000, 35.00000000000000000, 0.0000000000000000000 ) ) ;
#194 = FACE_OUTER_BOUND ( 'NONE', #159, .T. ) ;
#195 = ORIENTED_EDGE ( 'NONE', *, *, #205, .T. ) ;
#196 = CARTESIAN_POINT ( 'NONE', ( 0.0000000000000000000, 35.00000000000000000, 0.0000000000000000000 ) ) ;
...
我正在使用的代码:
for i in DoL.keys():
fobId = DoL["{}".format(i)]["FaceOuterBound"]
edgeloop_txt = re.findall(r'^#'+str(fobId)+r'.*#(\d+).*;', text)
print(edgeloop_txt)
DoL 是这样的字典:
{'Face0': {'AdvancedFace': 12, 'FaceOuterBound': 194, 'Plane': 326},
'Face1': {'AdvancedFace': 73, 'FaceOuterBound': 53, 'Plane': 230},
'Face2': {'AdvancedFace': 99, 'FaceOuterBound': 121, 'Plane': 123},
'Face3': {'AdvancedFace': 131, 'FaceOuterBound': 268, 'Plane': 270},
...
'Face9': {'AdvancedFace': 358, 'FaceOuterBound': 9, 'Plane': 363}}
【问题讨论】:
-
您的示例文本是什么样的?
-
@MCBama 示例文本为
#194 = FACE_OUTER_BOUND ( 'NONE', #159, .T. ) ; -
这一定要用正则表达式吗?
-
好的,您是从文件中读取还是文本对象已经存在?您可能想发布完整的代码 sn-p(堆栈帮助中心描述的 MCVE)
-
我看不出字典与这个问题有什么关系。基本的东西works
标签: python regex findall cad step