Transposing multiple lines of data into one table in Python

153 Views Asked by At

I have an output file from a script that parses iwlist scan that looks something like:

Cell: 01 -
Address: XX:XX:XX:XX:XX
ESSID: "My Network Name"
Frequency: 2.412 GHz (Channel 2)
Quality: =XX/100
Signal Level: XX/100

Cell: 02 -
Address: 
ESSID: 

etc etc for as many wlans that show up on the scan..

My question is how would I go about parsing this list even further, perhaps to a new file, to give it a tabulated view in the output (using python)?

for example, the output would be:

Cell          Address             ESSID                   Frequency          Quality    Signal Level
01 -    XX:XX:XX:XX:XX:XX    "My Network Name"      2.417 GHz (Channel 2)     =XX/100     =XX/100

etc for the rest of the wlans on the scan, without repeating the headers preferably.

1

There are 1 best solutions below

0
On

This would work, for example.

iwlist = '''Cell: 01 -
Address: XX:XX:XX:XX:XX
ESSID: "My Network Name"
Frequency: 2.412 GHz (Channel 2)
Quality: =XX/100
Signal Level: XX/100

Cell: 02 -
Address:
ESSID:
'''

options = []
values = []

for line in iwlist.split('\n'):
    if not line.strip():
        continue
    line = line.split(':')
    options.append(line[0])
    values.append(':'.join(line[1:]))

for o in options:
    print('{:^20}'.format(o), end="")
print()

for v in values:
    print('{:^20}'.format(v), end="")
print()