python - table formatting, print out row names -


i have dictionary want format table:

band3 = \ {'channel1': [10564, 2112, 1922],  'channel10': [10787, 2157, 1967],  'channel11': [10812, 2162, 1972],  'channel12': [10837, 2167, 1977],  'channel2': [10589, 2117, 1927],  'channel3': [10612, 2122, 1932],  'channel4': [10637, 2127, 1937],  'channel5': [10662, 2132, 1942],  'channel6': [10687, 2137, 1947],  'channel7': [10712, 2142, 1952],  'channel8': [10737, 2147, 1957],  'channel9': [10762, 2152, 1962]} 

i this:

table = [[], [], [], []] # can't sort channel names because 'channel11' < 'channel2' channel_numbers = [] channel_name in band3.keys():     if channel_name.startswith('channel'):         channel_number = int(channel_name[7:])         channel_numbers.append(channel_number)     else:         raise valueerror("channel name doesn't follow pattern") channel_numbers.sort()  channel_number in channel_numbers:     channel_data = band2['channel%d' % channel_number]     column =[               'channel %d' % channel_number,                str(channel_data[0]),                '%s/%s' % (channel_data[1], channel_data[2]),                str(channel_data[3])             ]     cell_widths = map(len, column) #9 5 2 9     column_widths = max(cell_widths) # 9 or 10     in range(len(cell_widths)): #4         cell = column[i]         padded_cell = cell + ' '*(column_widths-len(cell))         table[i].append(padded_cell) line in table:     print('  '.join(line)) 

this gives:

channel 1  channel 2  channel 3  channel 4  channel 5  channel 6  channel 7  channel 8  channel 9  channel 10  channel 11  channel 12 10564      10589      10612      10637      10662      10687      10712      10737      10762      10787       10812       10837      2112/1922  2117/1927  2122/1932  2127/1937  2132/1942  2137/1947  2142/1952  2147/1957  2152/1962  2157/1967   2162/1972   2167/1977  20         0          0          26         32         0          26         0          0          0           0           15        

however name rows:

       channel 1  channel 2  channel 3  channel 4  channel 5  channel 6  channel 7  channel 8  channel 9  channel 10  channel 11  channel 12 uarfcn 10564      10589      10612      10637      10662      10687      10712      10737      10762      10787       10812       10837      dl/ul  2112/1922  2117/1927  2122/1932  2127/1937  2132/1942  2137/1947  2142/1952  2147/1957  2152/1962  2157/1967   2162/1972   2167/1977  rssi   20         0          0          26         32         0          26         0          0          0           0           15         

this easy, change print loop this:

print "      ", print('  '.join(table[0]))  print "uarfcn", print('  '.join(table[1]))  print "dl/ul ", print('  '.join(table[2]))  print "rssi  ", print('  '.join(table[3]))  

however i'd know nicer ways print these column names. verbose padding calculations above, wondering nice clean, simple way.

edit: format attempt:

print('{0:6s}  {1}'.format("", ' '.join(table[0])))    print('{0:2s}  {1}'.format("uarfcn", ' '.join(table[1])))    print('{0:6s}  {1}'.format("dl/ul", ' '.join(table[2])))    print('{0:6s}  {1}'.format("rssi", ' '.join(table[3])))  

edit: way

print('{0} {1}'.format("".ljust(6), ' '.join(table[0])))    print('{0} {1}'.format("uarfcn".ljust(6), ' '.join(table[1])))    print('{0} {1}'.format("dl/ul".ljust(6), ' '.join(table[2])))    print('{0} {1}'.format("rssi".ljust(6), ' '.join(table[3])))   

improvement suggestions?

as many python tasks, more elegant code can achieved importing magical modules. in case, module called prettytable.

here working code:

from prettytable import prettytable  band3 = \ {'channel1': [10564, 2112, 1922],  'channel10': [10787, 2157, 1967],  'channel11': [10812, 2162, 1972],  'channel12': [10837, 2167, 1977],  'channel2': [10589, 2117, 1927],  'channel3': [10612, 2122, 1932],  'channel4': [10637, 2127, 1937],  'channel5': [10662, 2132, 1942],  'channel6': [10687, 2137, 1947],  'channel7': [10712, 2142, 1952],  'channel8': [10737, 2147, 1957],  'channel9': [10762, 2152, 1962]}  band3_new = {} key in band3.keys():     band3_new[ int( key.split('channel')[1] ) ] = band3[key]  x = prettytable() x.add_column("", ["uarfcn", "dl/ul"])  channel in band3_new.keys():     x.add_column("channel " + str(channel), [band3_new[channel][0], str(band3_new[channel][1]) + "/" + str(band3_new[channel][2]) ] )  print x 

and output:

+--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+------------+------------+------------+ |        | channel 1 | channel 2 | channel 3 | channel 4 | channel 5 | channel 6 | channel 7 | channel 8 | channel 9 | channel 10 | channel 11 | channel 12 | +--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+------------+------------+------------+ | uarfcn |   10564   |   10589   |   10612   |   10637   |   10662   |   10687   |   10712   |   10737   |   10762   |   10787    |   10812    |   10837    | | dl/ul  | 2112/1922 | 2117/1927 | 2122/1932 | 2127/1937 | 2132/1942 | 2137/1947 | 2142/1952 | 2147/1957 | 2152/1962 | 2157/1967  | 2162/1972  | 2167/1977  | +--------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+------------+------------+------------+ 

i couldn't figure out rssi row values from, though.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -