skip to Main Content

I need to split the path defined in below code in following a manner that

k[0] = /dev/mapper/centos-root
k[1] = 52403200
k[2] = 14460252 an so on.

What have I tried?
I tried using the split function but it currently takes k[0] = /
also, I tried passing it into a list where the whole context comes as one value

command = ("df -P | egrep '([20][0-9]|100)%' |  awk '{print}'|sed -e 's/ / : /g'")
command_execute = (stdin, stdout, stderr) = ssh.exec_command(command)
read_contents = stdout.read().decode("utf-8")
read_contents
'/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /n'
k=read_contents[0]
k
'/'
k=read_contents[1]
k
'd'
k.split(" ")
['d']
m =k.split(":")
m
['d']
s = [read_contents]
s
['/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /n']
s[0]
'/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /n'
s[1]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: list index out of range
s[0]
'/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /n'

k[0] = /dev/mapper/centos-root
k[1] = 52403200
k[2] = 14460252 an so on.

3

Answers


  1. You want to get rid of strings with whitespaces when you split on :

    In [20]: read_contents = '/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /n'                                                                                                 
    #Get rid of empty strings, and strip leading and trailing whitespaces around the rest of the words
    In [21]: contents = [content.strip() for content in read_contents.split(':') if content.strip()]                                                                                                                           
    
    In [22]: contents                                                                                                                                                                                                          
    Out[22]: ['/dev/mapper/centos-root', '52403200', '14460252', '37942948', '28%', '/']
    
    In [23]: for item in contents: 
        ...:     print(item) 
        ...:                                                                                                                                                                                                                   
    /dev/mapper/centos-root
    52403200
    14460252
    37942948
    28%
    /
    
    Login or Signup to reply.
  2. Another option is to use filter function to select by the expected numbers length.

    read_contents = '/dev/mapper/centos-root :  :  :  : 52403200 : 14460252 :  : 3794...: 2948 :  :  :  :  :  : 28% : /n'
    k = list(filter(lambda x: len(x)>5, read_contents.split(':')))
    

    Ouput:

    print(k)
    ['/dev/mapper/centos-root ', ' 52403200 ', ' 14460252 ', ' 3794...', ' 2948 ']
    
    Login or Signup to reply.
  3. import re
    a='/dev/mapper/centos-root :52403200 : 14460252 :  : 37942948 :  :  :  :  :  : 28% : /n'
    print(re.split(r'(?:s+:)+',a))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search