Python - 搜索字符串,从行中提取数字并附加到列表

时间:2017-12-10 20:17:19

标签: python cad step

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

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

1 个答案:

答案 0 :(得分:1)

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

结果将是Face ID = [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))