的数字我有以下内容的文件:查找文本
&ACCESS RVP1
&REL 3
&PARAM DISKPATH = KRC:\R1\Program\new
DEF SETUP_WELD()
;call LASER_EN();
$out[27]=false; START=OFF
$out[26]=false; STROBE=OFF
$out[25]=false; SELECT 8=OFF
$out[24]=false; SELECT 7 =OFF
$out[23]=false; SELECT 6 =OFF
$out[22]=false; SELECT 5 =OFF
$out[21]=false; SELECT 4 =OFF
$out[20]=false; SELECT 3 =OFF
$out[19]=false; SELECT 2 =OFF
$out[18]=false; SELECT 1 =OFF
$out[17]=false; SELECT 0 =OFF
$out[28]=true; ENABLE=ON
END
我需要找到值,即在括号[],并将其写入到阵列。 在结果,我应该得到下一个结果:
[ '27', '26','25','24','23','22','21','20','19','18','17','28']
我有使用Python这样做。我对此很新,请问,给我一个提示什么是最恰当的方法?
我的想法是这样的:我文件读入到数组,然后我在想在数组元素使用搜索:
def reading():
with open ('Setup_Weld.src') as f:
array1=[row.strip() for row in f]
但我不知道如何在搜索元素数组。
UPD:找到了答案。工作代码为:
def reading():
with open ('Setup_Weld.src') as f:
stripped=[row.strip() for row in f]
setup_weld_num = [re.search(r'\[(.*?)\]',i).group(1) for i in stripped if re.search(r'\[(.*?)\]',i)]
print(setup_weld_num)
reading()
正则表达式是一些谷歌搜索的选项。向我们展示您尝试过的代码? –
@Acapulka你的改变使得问题显得更加复杂并使现有答案失效。考虑恢复这些变化,并开始一个新的问题,你不知道如何处理额外的行。 – perigon