-1

I have written a python code to convert string into list and then compare status of each items.

Able to convert the list as: Each field is separated by more than one spaces(or tab):

['abc   2/2     Running   2          5h', 'def      3/3     Running   0          6h', 'xyz       1/1     Running   0          5h']
  1. I want to convert this into :
[['abc','2/2','Running','2','5h'], ['def','3/3','Running','0','6h'], ['xyz','1/1','Running','0',  '5h']]
  1. I want to verify all the status of the items(3rd field). If status of all 3 items are "Running", flag_variable is set to True else False.

     from pexpect import pxssh
     import pexpect
    
     def Login_Server(hostip,user,passwd):
       User_Info_out = []
       Cmd_Out = ' '
       try:
         Cmd = pxssh.pxssh()
         Cmd.login(hostip, user, passwd)
         Cmd_Out = Execute_Script4(Cmd)
    
       except pxssh.ExceptionPxssh as e:
         User_Info_out = 'None'
       return User_Info_out,Cmd_Out
    
     def Execute_Script4(Cmd):
         getpod_out,Cmd_Out1b = ' ', ' '
         pod_status = []
         print("********************")
         Cmd.sendline('sudo su ')
         Cmd.prompt()
         Cmd.sendline('kubectl get pods')
         Cmd.prompt()
         getpod_out  = Cmd.before
         pod_status = getpod_out.split()
         print("script output:")
         print (getpod_out)
         print("****************")
         getpod_out=getpod_out.splitlines()[3:-1]
         print(getpod_out)
         print(type(getpod_out))
    
     Login_Server('XXX.XXX.X.XX','usera','password')
    

    output:

     ********************
     script output:
     sudo su
     root@workernode8:/home/ubuntu# kubectl get pods
     NAME     READY   STATUS    RESTARTS   AGE
     abc      2/2     Running   2          5h
     def      3/3     Running   0          6h
     xyz      1/1     Running   0          5h
     root@workernode8:/home/ubuntu#
     ****************
     ['abc   2/2     Running   2          5h', 'def      3/3     Running   0          6h', 'xyz       1/1     Running   0          5h']
     <type 'list'>
    
Red
  • 26,798
  • 7
  • 36
  • 58
Lakshanth C
  • 95
  • 2
  • 9
  • 1
    read something about python `split` and try again, should be pretty straight forward – Marcus.Aurelianus Jul 28 '20 at 14:19
  • 2
    Does this answer your question? [How to split a string into a list?](https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list) – MLavrentyev Jul 28 '20 at 14:21
  • Can you clarify what exactly is your question? You already know about ``str.split``, what problem do you have splitting ``'abc 2/2 Running 2 5h'`` into ``['abc','2/2','Running','2','5h']``? What is ``flag_variable``? Do you know how do 2. if you were to have the result of 1.? – MisterMiyagi Jul 28 '20 at 14:39

1 Answers1

1

Here is how you can use split():

def check(lst):
    lst = [[i for i in s.split() if i] for s in lst]
    flag_variable = all(i[2]=='Running' for i in lst)
Red
  • 26,798
  • 7
  • 36
  • 58
  • Your function should probably `return` something. – Trenton McKinney Jul 28 '20 at 20:49
  • I can't be sure what the function is supposed to do. – Red Jul 28 '20 at 20:50
  • _I want to verify all the status of the items(3rd field). If status of all 3 items are "Running", flag_variable is set to True else False._ so it should return `flag_variable` as `True` or `False`. Or just `return all(i[2]=='Running' for i in lst)` – Trenton McKinney Jul 28 '20 at 20:52
  • Thanks. This code is working. When we pass the list to the function, it splits the data as expected and returns true if "Running" else False. – Lakshanth C Jul 29 '20 at 03:48