Skip to content Skip to sidebar Skip to footer

Splitlines In Python A Table With Empty Spaces

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

Solution 1:

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

Post a Comment for "Splitlines In Python A Table With Empty Spaces"