python通过正则表达式遍历列表(python loop through list through regex)

系统教程 行业动态 更新时间:2024-06-14 16:59:22
python通过正则表达式遍历列表(python loop through list through regex)

我试图用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.

更多推荐

本文发布于:2023-04-16 19:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a25325e3582637e8a36b00490b0967e5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   列表   正则表达式   python   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!