How can I join a string with his value?

送分小仙女□ 提交于 2020-06-29 04:05:12

问题


First of all, I have this string from the output of an SSH command (there are a lot of more nodes with all the volumes and dates:

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 

Then, I would like to take all those servers where their last-success-op-end is older than 7 days, and receive an output like this one:

xxx_xxx_xxx_xxxxx.trident_pvc_387e46bc_7fad_4424_95d4_ab15a3e156a8= 7 days;

I have tried doing this:

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)

With that, I receive an output like this one:

[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]

But I am not finding information about how could I associate those values to his vserver + volume and print it...

Is there a way to do that?

Thanks a lot for your help.


回答1:


Your results list is an a list that is equally long as the amount of lines you get from your ssh command output.

if you can retrieve the vserver + volume from the output as well as a list, you can use a list comprehension and a conditional to select the ones you are interested in:

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]

This selects all server accesses that are older than 7 days.




回答2:


an example how this could be done using the pandas DataFrame structure:

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]


来源:https://stackoverflow.com/questions/62449261/how-can-i-join-a-string-with-his-value