Python - 搜索字符串,从行中提取数字并附加到列表
Python - search for string, extract number from line and append to list
我正在使用一种 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
我该如何去除正则表达式匹配,以便只有数字附加到列表中?
您可以在正则表达式中使用组并在附加到面孔列表之前直接将字符串“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))
我正在使用一种 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
我该如何去除正则表达式匹配,以便只有数字附加到列表中?
您可以在正则表达式中使用组并在附加到面孔列表之前直接将字符串“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))