Splitlines in Python a table with empty spaces

2019-08-11 11:42发布

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!

1条回答
Juvenile、少年°
2楼-- · 2019-08-11 12:12

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).

import struct

lsof_list = """\
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
""".splitlines()

# note: variable-length NAME field at the end intentionally omitted
base_format = '8s 1x 6s 1x 10s 1x 4s 1x 9s 1x 6s 1x 9s 1x 6s 1x'
base_format_size = struct.calcsize(base_format)

for line in lsof_list:
    remainder = len(line) - base_format_size
    format = base_format + str(remainder) + 's'  # append NAME field format
    fields = struct.unpack(format, line)
    print fields

Output:

('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')
查看更多
登录 后发表回答