【问题标题】:How can I join a string with his value?我怎样才能用他的值加入一个字符串?
【发布时间】:2020-10-08 11:13:59
【问题描述】:

首先,我从 SSH 命令的输出中获得了这个字符串(还有很多节点,包含所有卷和日期:

vserver           volume                                           last-success-op-end      
----------------- ------------------------------------------------ ------------------------ 
xxx_xxx_xxx_xxxxx trident_pvc_387e46bc_7fad_4424_95d4_ab15a3e156a8 Mon Jun 10 16:52:18 2020 
xxx_xxx_xxx_xxxxx trident_pvc_42816b6e_cd61_4929_a7c2_41de3f593c23 Mon Jun 15 16:52:35 2020 
xxx_xxx_xxx_xxxxx trident_pvc_5932a33a_ca9f_4131_8d2b_e465f195c633 Mon Jun 15 16:52:29 2020 
xxx_xxx_xxx_xxxxx trident_pvc_769d0605_1964_4dfe_9792_1d84e331519f Mon Jun 15 18:25:30 2020 

然后,我想获取所有那些最后一次成功操作结束时间超过 7 天的服务器,并收到这样的输出:

xxx_xxx_xxx_xxxxx.trident_pvc_387e46bc_7fad_4424_95d4_ab15a3e156a8= 7 days;

我试过这样做:

import subprocess
import argparse
import sys
import re
import datetime
from subprocess import check_output

command = #ssh command that returns me that string 
output = check_output(command, shell=True)

#Here I take all the dates
dates_1 = str(re.findall('(Mon.*|Sun.*|Tue.*|Wed.*|Thu.*|Fri.*|Sat.*|Sun.*)', output)).replace("\\r", "").replace(" '", "'").replace("'", "").replace("[", "").replace("]", "")
dateslist = dates_1.split(",")


dates_list = [datetime.datetime.strptime(date, "%a %b %d %H:%M:%S %Y").date() for date in dateslist]

now = datetime.datetime.now().date()

deltas = [now-d for d in dates_list]

delta_days = [td.days for td in deltas]

results = map(int, delta_days)


print (results)

这样,我会收到这样的输出:

[3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 3, 5, 5, 4, 4, 4, 4, 4, 4, 0, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 17, 17, 0, 0, 0, 0, 0, 10, 8, 3, 3, 4, 4, 4, 4, 4, 3, 7, 56, 0, 2, 28, 17, 1, 4, 2, 0, 2, 2, 2, 37, 2, 2, 2, 6, 2, 2, 2, 3, 2, 0, 2, 0, 2, 7, 0, 0, 1032, 0, 0, 26, 4, 3, 4, 4, 4, 0, 6, 4, 645, 241, 141, 141, 322, 303, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

但我没有找到有关如何将这些值与他的 vserver + 卷关联并打印它的信息...

有没有办法做到这一点?

非常感谢您的帮助。

【问题讨论】:

  • 您是否考虑过将输出字符串output 解析为pandas 数据帧?在我看来,这可能是一个实用的选择......你能添加几行,output 的样子吗?
  • 您好@MrFuppes,我的问题开头有一个输出示例
  • 好的,我会试着举一个例子。每行是否由换行符分隔?
  • 嘿@MrFuppes 是的,每一行都用换行符分隔

标签: python regex string datetime parsing


【解决方案1】:

您的results 列表是一个与您从ssh 命令输出中获得的行数一样长的列表。

如果您可以从 output 以及列表中检索 vserver + 卷,则可以使用列表推导和条件来选择您感兴趣的内容:

vserver = str(re.findall(.... # here you would need to identify all vserver accesses
vserver_select = [vs for (vs, i) in enumerate(vserver) if result[i] > 7]

这会选择所有超过 7 天的服务器访问。

【讨论】:

    【解决方案2】:

    如何使用pandas DataFrame 结构来完成此操作的示例:

    from io import StringIO
    import pandas as pd
    
    # example input
    s = ("vserver           volume                                           last-success-op-end     \n"
         "----------------- ------------------------------------------------ ------------------------\n"
         "xxx_xxx_xxx_xxxxx trident_pvc_387e46bc_7fad_4424_95d4_ab15a3e156a8 Mon Jun 10 16:52:18 2020\n"
         "xxx_xxx_xxx_xxxxx trident_pvc_42816b6e_cd61_4929_a7c2_41de3f593c23 Mon Jun 15 16:52:35 2020\n")
    
    # remove the first two lines with s.split('\n')[2:]
    # update the separators in the input to get 3 columns (replace first two spaces with ,)
    s = '\n'.join([','.join(l.split(' ', 2)) for l in s.split('\n')[2:]]).strip()
    
    # load input to DataFrame
    df = pd.read_csv(StringIO(s), names=['vserver', 'volume', 'last-success-op-end'])
    
    # parse last-success-op-end column to datetime
    df['last-success-op-end'] = pd.to_datetime(df['last-success-op-end'], format="%a %b %d %H:%M:%S %Y")
    
    # now you can easily calculate the 'age' of a log entry against a reference date:
    ref_date = pd.to_datetime('today')
    df['age_days'] = (ref_date-df['last-success-op-end']).dt.total_seconds() / 86400
    
    # df
    #              vserver  ...  age_days
    # 0  xxx_xxx_xxx_xxxxx  ...  7.915218
    # 1  xxx_xxx_xxx_xxxxx  ...  2.915021
    #
    # [2 rows x 4 columns]
    

    【讨论】:

    • 你好df = pd.read_csv(StringIO(s), names=['vserver', 'volume', 'last-success-op-end']) TypeError: initial_value must be unicode or None, not str
    • @jmgalan:似乎是 subprocess.check_output() 的返回值有问题。试试我的例子s = str(check_output(command, shell=True))
    • @jmgalan:如果我不知道您的check_output(command, shell=True)确切返回类型和值,我担心我们不会取得太大进展,抱歉...
    猜你喜欢
    • 2011-01-10
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2016-10-16
    • 2014-12-14
    相关资源
    最近更新 更多