Python 要列出的同一字符串的多个实例
我有一个子进程命令Python 要列出的同一字符串的多个实例,python,regex,Python,Regex,我有一个子进程命令 temp=子流程。检查输出(cmd) 这将返回一个单个字符串 字符串将包含具有许可证的每个记录的数据 比如说, 如果当前只有一条记录具有许可证:字符串如下所示: b'Setting license file path to 5053@100.113.111.61\r\n\r\n\t------------------------\r\n\r\n\tredgiant license usage status on 100.113.248.61 (port 55952)\r\n\
temp=子流程。检查输出(cmd)
这将返回一个单个字符串
字符串将包含具有许可证的每个记录的数据
比如说,
如果当前只有一条记录具有许可证:字符串如下所示:
b'Setting license file path to 5053@100.113.111.61\r\n\r\n\t------------------------\r\n\r\n\tredgiant license usage status on 100.113.248.61 (port 55952)\r\n\r\n\tmagicbulletlooks v999.9: administrator@nynle650 1/0 at 02/29 09:51 (handle: 62)\r\n\r\n'
b'Setting license file path to 5053@100.113.111.61\r\n\r\n\t--------------------
----\r\n\r\n\tredgiant license usage status on 100.113.111.61 (port 55952)\r\n\r
\n\tmagicbulletlooks v999.9: administrator@nynle650 1/0 at 02/29 11:42 (handle:
68)\r\n\tmagicbulletlooks v999.9: administrator@nynle647 1/0 at 02/29 11:46 (h
andle: 8d)\r\n\r\n'
如果有两个具有许可证的记录,它将如下所示:
b'Setting license file path to 5053@100.113.111.61\r\n\r\n\t------------------------\r\n\r\n\tredgiant license usage status on 100.113.248.61 (port 55952)\r\n\r\n\tmagicbulletlooks v999.9: administrator@nynle650 1/0 at 02/29 09:51 (handle: 62)\r\n\r\n'
b'Setting license file path to 5053@100.113.111.61\r\n\r\n\t--------------------
----\r\n\r\n\tredgiant license usage status on 100.113.111.61 (port 55952)\r\n\r
\n\tmagicbulletlooks v999.9: administrator@nynle650 1/0 at 02/29 11:42 (handle:
68)\r\n\tmagicbulletlooks v999.9: administrator@nynle647 1/0 at 02/29 11:46 (h
andle: 8d)\r\n\r\n'
随着数量的增长,等等
我正在尝试提取magicbulletlooks v999.9:administrator@nynle6471/0在02/29 11:46
部分,与列表中出现的次数相同
对于每次事件,列表中应有一项
目前我正在使用
def do_work():
regex= re.compile("magicbulletlooks(.*\))")
t = subprocess.check_output(my_cmd)
return re.findall(regex,str(t))
但是,这只会返回一个包含1个值的列表,该列表从头到尾都有完整的字符串,并且不存储单个事件
基本上,我试图达到的目标是使用regex,创建以下列表:
['magicbulletlooks v999.9 administrator@nynle647 1/0 at 2/29 11:46',
'magicbulletlooks v999.9 administrator@nynle650 1/0 at 2/29 11:42'
]
作为对您的模式的修复,以下是我可以想到的:
>>> re.findall('magicbulletlooks.*\d+', s)
['magicbulletlooks v999.9: administrator@nynle650 1/0 at 02/29 11:42', 'magicbulletlooks v999.9: administrator@nynle647 1/0 at 02/29 11:46']
现在在您的功能中:
def do_work():
pat = re.compile('magicbulletlooks.*\d+')
t = subprocess.check_output(my_cmd)
return pat.findall(str(t))
编辑:
引用文件:
子流程。检查_输出(args,*,stdin=None,stderr=None,shell=False,
universal_newlines=False,timeout=None)
使用参数运行命令并返回其输出
这相当于:
默认情况下,此函数将以编码字节的形式返回数据。这个
输出数据的实际编码可能取决于正在执行的命令
调用,因此对文本的解码通常需要在
应用程序级别。
可以通过将universal_换行符设置为True来覆盖此行为
如上在常用参数中所述。
这里有两种选择:
1-将universal\u新行
标志设置为True
如下:
t = subprocess.check_output(my_cmd, universal_flag=True)
pat = re.compile('magicbulletlooks.*\d+')
return pat.findall(t)
2-将re
表达式中的模式指定为类二进制对象:
pat = re.compile(b'magicbulletlooks.*\d+')
t = subprocess.check_output(my_cmd)
return pat.findall(t)
3-将类二进制
对象解码为utf-8
(如果处理Python3,则更可取)或ascii
字符串:
>>> t = subprocess.check_output(my_cmd)
>>>
>>> re.findall('magicbulletlooks.*\d+', t.decode('utf-8'))
['magicbulletlooks v999.9: administrator@nynle650 1/0 at 02/29 11:42', 'magicbulletlooks v999.9: administrator@nynle647 1/0 at 02/29 11:46']
>>>
>>> re.findall('magicbulletlooks.*\d+', t.decode('ascii'))
['magicbulletlooks v999.9: administrator@nynle650 1/0 at 02/29 11:42', 'magicbulletlooks v999.9: administrator@nynle647 1/0 at 02/29 11:46']
我认为这是因为你从子流程中获得了输出。检查输出作为字节状对象,然后尝试使用这个表达式:pat=re.compile(b'magicbulletlooks.*\d+)
注意magicbulletlooks
之前的b
,谢谢mate@Busturdust .. 很高兴能帮上忙:)。。检查更新的答案…我也为您添加了几个选项。