繁体   English   中英

使用子进程 python 获取日期?

[英]Get date using subprocess python?

我使用 subprocess 来获取有关我的 VM 中目录的信息。

import subprocess

output = subprocess.getoutput("ssh -i /path-to-key ubuntu@IP ls -l /opt/orientdb/databases")
print(output)

我得到 output 作为

drwxr-xr-x 2 root root  4096 Apr 25  2019 friendship_graph_434
drwxr-xr-x 2 root root  4096 Jul 18  2019 friendship_graph_453
drwxr-xr-x 2 root root  4096 Sep  3  2019 friendship_graph_465
drwxr-xr-x 2 root root  4096 Oct  2  2019 friendship_graph_468
drwxr-xr-x 2 root root  4096 Oct  4  2019 friendship_graph_471
drwxr-xr-x 2 root root  4096 Oct 15  2019 friendship_graph_477
drwxr-xr-x 2 root root  4096 Nov  6  2019 friendship_graph_471
drwxr-xr-x 2 root root  4096 Apr 29  2020 friendship_graph_496
drwxr-xr-x 2 root root  4096 Nov 27  2019 friendship_graph_497
drwxr-xr-x 2 root root  4096 Dec  3  2019 friendship_graph_49
drwxr-xr-x 2 root root  4096 Dec  4  2019 friendship_graph_498

我怎样才能从上面的 output 中得到日期? 谢谢

您可以使用正则表达式来搜索日期。

from datetime import datetime
import re

pattern = re.compile(
    r"\b((?:(?:Jan)|(?:Feb)|(?:Mar)|(?:Apr)|(?:May)|(?:Jun)|(?:Jul)|(?:Aug)|(?:Sep)|(?:Oct)|(?:Nov)|(?:Dec))\s+\d\d?\s+(?:[12]\d\d\d))\b"
)

format = '%b %d  %Y'

data = '''\
drwxr-xr-x 2 root root  4096 Apr 25  2019 friendship_graph_434
drwxr-xr-x 2 root root  4096 Jul 18  2019 friendship_graph_453
drwxr-xr-x 2 root root  4096 Sep  3  2019 friendship_graph_465
drwxr-xr-x 2 root root  4096 Oct  2  2019 friendship_graph_468
drwxr-xr-x 2 root root  4096 Oct  4  2019 friendship_graph_471
drwxr-xr-x 2 root root  4096 Oct 15  2019 friendship_graph_477
drwxr-xr-x 2 root root  4096 Nov  6  2019 friendship_graph_471
drwxr-xr-x 2 root root  4096 Apr 29  2020 friendship_graph_496
drwxr-xr-x 2 root root  4096 Nov 27  2019 friendship_graph_497
drwxr-xr-x 2 root root  4096 Dec  3  2019 friendship_graph_49
drwxr-xr-x 2 root root  4096 Dec  4  2019 friendship_graph_498
'''

lines = data.splitlines()
for line in lines:
    m = pattern.search(line)
    s = m.group(1)
    d = datetime.strptime(s, format)
    print(d.date())

作为替代方案,既然您已经在使用子进程,不妨一直使用它:

import subprocess

output = subprocess.getoutput("ssh -i /path-to-key ubuntu@IP ls -l /opt/orientdb/databases | awk -F' ' '{print $6, $7, $8}'")
print(output)

应该让你得到 output 的格式:

2019 年 4 月 25 日

2019 年 7 月 18 日

等等

所以解释我做了什么。 我在您的命令中添加了以下内容: "| awk -F' ' 'print{$6, $7, $8}'"

第一个令牌: | 是一个 pipe,基本上是说使用最后一个命令的 output,在这种情况下ls作为下一个命令的输入,在这种情况下awk

awk太棒了。 在这种情况下,我正在做的是用-F ' '指定一个分隔符,表示根据空格分割提供的行。 awk 似乎可以很好地处理有多个空格的场景(例如,在 April_25 和 Feb__2 之间)

然后'print{$6, $7, $8}'表示基于该分隔符,打印出第 6、7 和 8 列,它们应对应于您的月/日/年

从那里开始,在 python 中进行非常简单的日期处理,如其他答案中所述。 我们将使用 datetime 库的 strptime function,它采用字符串和某种格式,并返回您可以操作和使用的日期时间 object。 查看格式代码后,该格式非常易于使用

lines = data.splitlines()
format = '%b %d  %Y'# Will parse strings like April 25 2019
# format = '%Y-%m-%d' will parse strings like 2019-04-25

for line in lines:
    date = datetime.strptime(line, format)
    print(date)
    # should get something like datetime.datetime(2019, 4, 25, 0, 0)

你可以有一些这样的代码:

import calendar

output_split = output.split("\n")
dates = []
months = {month: index for index, month in enumerate(calendar.month_abbr) if month}
for i in output_split:
    y = i[29:-21].split()
    dates.append(f"{y[1]}/{months[y[0]]}/{y[2]}")
print(dates)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM