[英]Python re.findall expected string
I want to search for a word in my string with re.findall
.我想用
re.findall
在我的字符串中搜索一个单词。 The Code is the following:代码如下:
def check_status():
p1 = subprocess.run(["screen", "-ls"], shell=False)
p2 = re.findall("myscreen", p1)
print(p2)
The return from p1
looks like this: p1
的返回如下所示:
There are screens on:
2420454.myscreen (12/25/2021 01:15:17 PM) (Detached)
6066.bot (12/14/2021 07:11:52 PM) (Detached)
If I execute this function, I get the following error message:如果我执行此 function,我会收到以下错误消息:
File "/usr/lib/python3.10/re.py", line 240, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object
I searched for this problem already but found nothing.我已经搜索过这个问题,但一无所获。
I'm using Python 3.10我正在使用 Python 3.10
subprocess.run
returns a CompletedProcess
object. subprocess.run
返回CompletedProcess
object。 You want to add capture_output=True
to the keyword arguments, and apply the regular expression to its stdout
member:您想将
capture_output=True
添加到关键字 arguments,并将正则表达式应用于其stdout
成员:
p2 = re.findall('myprocess', p1.stdout)
.. though of course. ..当然。 you don't need a regular expression to look for a static string:
您不需要正则表达式来查找 static 字符串:
p2 = 'myprocess' in p1.stdout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.