Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:将str的一部分保存到数组_Python_Variables_Search - Fatal编程技术网

Python:将str的一部分保存到数组

Python:将str的一部分保存到数组,python,variables,search,Python,Variables,Search,我在python中遇到了一个问题。我有一个变量集作为命令的输出,这个输出有很多行。为了简单起见,让我们说这是命令的输出,我有问题,现在存储在变量“OUTPUT”中: FRU 0_2_3附件1FRU 0_2_4附件1FRU 0_2_5附件1FRU 0_2_6附件1 现在我希望搜索该变量并只存储表示#####的部分行注意:我不想为此使用文本文件 import re disks=[] i=0 OUTPUT=fbeclicommand("li -all") #This is the command on

我在python中遇到了一个问题。我有一个变量集作为命令的输出,这个输出有很多行。为了简单起见,让我们说这是命令的输出,我有问题,现在存储在变量“OUTPUT”中:

FRU 0_2_3附件1
FRU 0_2_4附件1
FRU 0_2_5附件1
FRU 0_2_6附件1


现在我希望搜索该变量并只存储表示#####的部分行
注意:我不想为此使用文本文件

import re
disks=[]
i=0
OUTPUT=fbeclicommand("li -all") #This is the command on some exe that will gather the output and save it to this variable.
if re.search("\d{1,3}_\d{1,3}_\d{1,3}",OUTPUT):
    disks[i]=OUTPUT
    i+=1

我基本上只想在数组中存储数字部分,然后对所有4个数字进行存储,或者不管有多少个数字,您都可以将其按空格分割,得到索引1处的数字。您还可以使用列表上的
.append
方法将其推送到数组中,而不是使用标记并将其设置在某个索引处。同样的影响,更少的代码

import re
disks=[]

OUTPUT = '''
FRU 0_2_3 enclosure 1
FRU 0_2_4 enclosure 1
FRU 0_2_5 enclosure 1
FRU 0_2_6 enclosure 1
'''
#OUTPUT=fbeclicommand("li -all") #This is the command on some exe that will gather the output and save it to this variable.
OUTPUTSplit= OUTPUT.split('\n')
for line in OUTPUTSplit:
    if re.search("\d{1,3}_\d{1,3}_\d{1,3}", line):
        disks.append(line.split(' ')[1])

print disks
输出
我有点认为,如果您已经在使用正则表达式,您应该继续使用捕获组:

match = re.search("\d{1,3}_\d{1,3}_\d{1,3}", output_line):
if match is not None:
    disks.append(match.group())

@bladexeon很高兴我能帮忙
match = re.search("\d{1,3}_\d{1,3}_\d{1,3}", output_line):
if match is not None:
    disks.append(match.group())