我正在使用一种 STEP 文件格式,我想对其进行解析、提取信息并将其存储在数组中,以便稍后在程序中调用并对它们执行数学运算。
下面是我正在使用的数据示例(advanced_face 稍后在数据文件中引用 face_outer_bound:
#12 = ADVANCED_FACE ( 'NONE', ( #194 ), #326, .F. ) ;
...
#194 = FACE_OUTER_BOUND ( 'NONE', #159, .T. ) ;
到目前为止,这是我想出的:
import re
with open('TestSlot.STEP', 'r') as step_file:
data = step_file.readlines()
NF = 0
faces = []
for line in data:
line = line.strip()
if re.search("ADVANCED_FACE", line):
NF = NF + 1
advface = re.compile('#\d+')
advfaceresult = advface.match(line)
faces.append(advfaceresult.group())
print("Face IDs =", faces)
print("Number of faces, NF =", NF)
这给出了输出:
Face IDs = ['#12', '#73', '#99', '#131', '#181', '#214', '#244',
'#273', '#330', '#358']
Number of faces, NF = 10
我将如何剥离正则表达式匹配,以便仅将数字附加到列表中?