簡體   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