0

I need to create a table to display multiple sets of data that was collected with different conditions. This is what I need the table to look like:

        Config 1        Config 2
Test    Data 1  Data 2  Data 1  Data 2
abc     123     123     123     123

So the system was setup with configuration 1 and then two sets of data were collected. The system is reset with configuration 2 and then the same to sets of data are collected.

I have been trying to use prettytable but haven't found anything that specifies how to make a second header that applies to multiple of the following columns.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
BobJoe1803
  • 49
  • 1
  • 8
  • Just to be clear: You already have the data, but need a way to display them? If you need help with the data structure, I can help. Unfortunately I haven't done much display work in python, so it'll be harder for me to help if that is the case... – jumps4fun Jan 15 '16 at 16:12
  • I have a file with some data that has to be analyzed first, on that front I am good thanks. – BobJoe1803 Jan 15 '16 at 16:21

1 Answers1

2

You could write a function to align all of your data entries using the maximum column widths as follows:

def write_cols(data):
    col_spacer = "   "      # added between columns
    widths = [0] * len(data[0])

    # Calculate the widest entry for each column
    for row in data:
        widths[:] = [max(widths[index], len(str(col))) for index, col in enumerate(row)]

    return [col_spacer.join("{:<{width}}".format(col, width=widths[index]) for index, col in enumerate(row)) for row in data]

data = [['', 'Config 1', '', 'Config 2', ''], ["Test", "Data 1", "Data 2", "Data 1", "Data 2"], ["abc", "123", "123", "123", "123"]]

for row in write_cols(data):
    print row

This would display your data as follows:

       Config 1            Config 2         
Test   Data 1     Data 2   Data 1     Data 2
abc    123        123      123        123
Martin Evans
  • 45,791
  • 17
  • 81
  • 97