Through one command linux (lsof) I get a serie of data in a table:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd unknown /proc/1/cwd (readlink: Permission denied)
init 1 root rtd unknown /proc/1/root (readlink: Permission denied)
python 30077 user1 txt REG 8,1 2617520 461619 /usr/bin/python2.6
As we can see some places are emptly, Im looking the way to split every cell into a list (one list per row), im trying in this way:
lsof_list = commands.getoutput('lsof | sed \'1d\'')
k = 1
list_lsof = {}
j = 1
apps = {}
for line in lsof_list.splitlines():
list_lsof[k] = line
print(list_lsof[k])
for apps[j] in list_lsof[k].split():
#print(' app ', list_lsof[j])
j += 1
#print(apps[k])
if j == 9:
print(apps[1], apps[2], apps[3], apps[4], apps[5], apps[6], apps[7],apps[8])
j = 1
But due there are different long of spaces I got a lot of emptly " " items, how can i avoid it? I guess the problem is in lsof_list.splitlines(): but I cant find what I need to change. Thank you!
Since each line of output is almost a record of fixed-width fields, you could do something like the following to parse it (which is based on what is in another answer of mine). I determined the width of the fields manually because of the difficulty involved of determining it automatically primarily due to the mixing of left- and right-justified fields (as well as the lone variable length one at the end).
Output: