简体   繁体   English

使用子进程 python 获取日期?

[英]Get date using subprocess python?

I used subprocess to get information on a directory in my VM.我使用 subprocess 来获取有关我的 VM 中目录的信息。

import subprocess

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

and i get the output as我得到 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

How can i get just the date from the above output?我怎样才能从上面的 output 中得到日期? Thank you谢谢

You can use a regular expression to search for the dates.您可以使用正则表达式来搜索日期。

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())

As an alternative, since you are already using a subprocess might as well use it all the way:作为替代方案,既然您已经在使用子进程,不妨一直使用它:

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)

should get you output in the format:应该让你得到 output 的格式:

Apr 25 2019 2019 年 4 月 25 日

Jul 18 2019 2019 年 7 月 18 日

etc.等等

So to explain what I did.所以解释我做了什么。 I added the following to your command: "| awk -F' ' 'print{$6, $7, $8}'"我在您的命令中添加了以下内容: "| awk -F' ' 'print{$6, $7, $8}'"

The first token: |第一个令牌: | is a pipe, which is basically saying use the output of the last command, in this case ls as the input for the next command, in this case awk是一个 pipe,基本上是说使用最后一个命令的 output,在这种情况下ls作为下一个命令的输入,在这种情况下awk

awk is awesome. awk太棒了。 In this case, what I'm doing is specifying a delimiter with -F ' ' , saying to split the lines provided based on space.在这种情况下,我正在做的是用-F ' '指定一个分隔符,表示根据空格分割提供的行。 awk seems to handle the scenarios where there are multiple spaces well enough (Say between April_25 and Feb__2 for example) awk 似乎可以很好地处理有多个空格的场景(例如,在 April_25 和 Feb__2 之间)

then 'print{$6, $7, $8}' is saying that based on that delimiter, print out the 6th, 7th, and 8th columns, which should correspond to your month / day / year然后'print{$6, $7, $8}'表示基于该分隔符,打印出第 6、7 和 8 列,它们应对应于您的月/日/年

From there it's very simple date processing in python as mentioned in other answers.从那里开始,在 python 中进行非常简单的日期处理,如其他答案中所述。 We'll use the datetime library's strptime function, which takes a string, and some format, and returns a datetime object you can manipulate and use.我们将使用 datetime 库的 strptime function,它采用字符串和某种格式,并返回您可以操作和使用的日期时间 object。 The format is pretty straightforward to use after you check out the format codes查看格式代码后,该格式非常易于使用

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)

You can have some code like this:你可以有一些这样的代码:

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