1

我正在使用一种 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

我将如何剥离正则表达式匹配,以便仅将数字附加到列表中?

4

1 回答 1

1

您可以在正则表达式中使用组,并在附加到面列表之前将字符串“12”直接转换为数字 12 advface = re.compile('#(\d+)') advfaceresult = advface.match(line) faces.append(int(advfaceresult.group(1)))

结果将是 Face IDs = [12, ...]

也可以通过以下方式获得解决方案

import re
ifile = r'TestSlot.STEP'
with open(ifile) as f:
    text = f.read()  # read all text
    faces_txt = re.findall(r'#(\d+) = ADVANCED_FACE.*;', text)
    #  get all groups by re
    faces = [int(face) for face in faces_txt]   # convert to int
    print('Face IDs = ', faces)
    print('Number of faces, NF =', len(faces))
于 2017-12-10T20:27:27.570 回答