【发布时间】:2019-08-20 10:37:51
【问题描述】:
我正在尝试从终端输出中获取时间值。
import os
import re
cmd = os.popen('time Open /Applications/TextEdit.app').read()
time = re.search("real [0-9]{1}", cmd)
print(time)
但是找不到。
输出
real 0m0.042s
user 0m0.015s
sys 0m0.013s
None
我什至不能得到 0。我怎样才能得到 0.042 作为我的时间变量?
【问题讨论】:
-
time = re.search("^real\s+(\S+)$", cmd, re.M).group(1) -
我得到“AttributeError: 'NoneType' 对象没有属性 'group'”
-
你的
output是cmd的内容吗?请发布cmd的内容 -
@WiktorStribiżew 您显然是一位不需要提高声誉的正则表达式大师。但是,通过实际创建答案而不是仅仅发表评论以便可以接受并且标记为已回答的问题,您不会帮这个网站一个忙吗?
-
real 0m0.045s user 0m0.016s sys 0m0.013s Traceback (most recent call last): File "demotest", line 7, in <module> time = re.search("^real\s+(\S+)$", cmd, re.M).group(1) AttributeError: 'NoneType' object has no attribute 'group'
标签: python regex macos terminal