我试图用for循环迭代列表,并通过正则表达式捕获组构建第二个列表。 我查看了与re模块相关的文档但由于某种原因,正则表达式总是返回一个结果。 正则表达式经过测试,所以我确信这是有效的。
有什么想法吗?
编辑
#! /usr/bin/python import re import subprocess try: cmd = "ps aux|grep -i \'zabbi[x]\'" cmd_stdout = subprocess.check_output(cmd, shell=True).split('\n') cmd_stdout_lst = [] ps_re = re.compile(r'^(\S+)\s+(\d+)\s+\d+\.\d+\s+\d+\.\d+\s+(\d+)\s+(\d+).+') for line in cmd_stdout: match = ps_re.findall(line) if match: print match cmd_stdout_lst.append('\n\t\t{"{#USER}":'+'"' + match[0][0] + ',"{#PID}":'+'"'+match[0][1]+'"}'+',"{#PID}":'+'"'+match [0][2]+'"}'+',"{#PID}":'+'"'+match[0][3]+'"}') print '{\n\t"data":['+','.join(cmd_stdout_lst)+']\n}' except: raise $ python proc_discovery.py [('zabbix', '14479', '96784', '680')]I am trying to iterate a list with for loop and build a second list through regex capture group. I looked through documentation related to the re module but for some reason, the regex always return one result. The regex was tested so I am certain that is works.
Any thoughts?
Edited
#! /usr/bin/python import re import subprocess try: cmd = "ps aux|grep -i \'zabbi[x]\'" cmd_stdout = subprocess.check_output(cmd, shell=True).split('\n') cmd_stdout_lst = [] ps_re = re.compile(r'^(\S+)\s+(\d+)\s+\d+\.\d+\s+\d+\.\d+\s+(\d+)\s+(\d+).+') for line in cmd_stdout: match = ps_re.findall(line) if match: print match cmd_stdout_lst.append('\n\t\t{"{#USER}":'+'"' + match[0][0] + ',"{#PID}":'+'"'+match[0][1]+'"}'+',"{#PID}":'+'"'+match [0][2]+'"}'+',"{#PID}":'+'"'+match[0][3]+'"}') print '{\n\t"data":['+','.join(cmd_stdout_lst)+']\n}' except: raise $ python proc_discovery.py [('zabbix', '14479', '96784', '680')]最满意答案
我发现这有两个问题:
你的grep正则表达式与zabbix简单相同。 为什么额外[ ] ? 循环内第一次print后,程序崩溃。 您没有注意到,因为您捕获并忽略该异常。 删除try - except获得更有用的错误消息。具体来说,问题2是re.findall()返回tuple list 。 外部list在您的情况下有一个元素,内部tuple有四个。 当您尝试为第二个print语句构建字符串时,实际上是将整个元组连接到前缀字符串,这会导致异常。 使用match[0][0]代替match[0] ,依此类推,以解决此问题。
I see two things wrong with this:
Your grep regex matches the same thing as simply zabbix would. Why the extra [ ]? Your program crashes after the first print inside the loop. You don't notice because you catch and ignore the exception. Remove the try-except to get a more helpful error message.Specifically, problem number 2 is that re.findall() returns a list of tuples. The outer list has a single element in your case, the inner tuples have four. When you try to build the string for the second print statement, you are actually concatenating the entire tuple to the prefix string, which causes the exception. Use match[0][0] instead ofmatch[0] and so on, to fix this.
更多推荐
发布评论