Search code examples
pythonpretty-print

pretty printer with Python?


I have a list of labels, and data as follows.

['id', 'Version', 'chip_name', 'xversion', 'device', 'opt_param', 'place_effort']
[1, 1.0, u'virtex2', u'xilinx11.5', u'xc5vlx50', u'Speed', u'High']

I need to print them into console. And for this, I'm iterating over the list, and print out each element with a tab ('\t').

But, unfortunately, the result is not so pretty.


number of data 1 and number of column 7
id      Version     chip_name       xversion        device      opt_param       place_effort        
1       1.0     virtex2     xilinx11.5      xc5vlx50        Speed       High        

The string length of label and data is quite variable, and it's not aligned well.

Is there any solution to this problem with Python?

ADDED

Hepled by Mike DeSimone's answer, I could make the pretty printer that I can use for my purposes. The valueResults are a list of duple.

    labels = queryResult.names
    valueResults = queryResult.result

    # get the maximum width
    allData = valueResults
    allData.insert(0,labels)
    transpose = zip(*valueResults) # remove the sequence as a parameter
    #print transpose
    for value in transpose:
        # value is integer/float/unicode/str, so make it length of str
        newValue = [len(str(i)) for i in value]
        columnWidth = max(newValue)
        columnWidths.append(columnWidth)
        dividers.append('-' * columnWidth)
        dblDividers.append('=' * columnWidth)
        label = value[0]
        paddedLabels.append(label.center(columnWidth))

    paddedString = ""

    for values in valueResults[1:]:
        paddedValue = []
        for i, value in enumerate(values):
            svalue = str(value)
            columnWidth = columnWidths[i]
            paddedValue.append(svalue.center(columnWidth))
        paddedString += '| ' + ' | '.join(paddedValue) + ' |' + '\n'

    string += '+-' + '-+-'.join(dividers) + '-+' + '\n'
    string += '| ' + ' | '.join(paddedLabels) + ' |' + '\n'
    string += '+=' + '=+='.join(dblDividers) + '=+' + '\n'
    string += paddedString
    string += '+-' + '-+-'.join(dividers) + '-+' + '\n'

And this is the result.


+----+---------+-----------+------------+----------+-----------+--------------+
| id | Version | chip_name |  xversion  |  device  | opt_param | place_effort |
+====+=========+===========+============+==========+===========+==============+
| 1  |   1.0   |  virtex2  | xilinx11.5 | xc5vlx50 |   Speed   |     High     |
| 2  |   1.0   |  virtex2  | xilinx11.5 | xc5vlx50 |   Speed   |     High     |
+----+---------+-----------+------------+----------+-----------+--------------+

Thanks for the help.


Solution

  • Something like this:

    labels = ['id', 'Version', 'chip_name', 'xversion', 'device', 'opt_param', 
        'place_effort']
    values = [1, 1.0, u'virtex2', u'xilinx11.5', u'xc5vlx50', u'Speed', u'High']
    
    paddedLabels = []
    paddedValues = []
    
    for label, value in zip(labels, values):
        value = str(value)
        columnWidth = max(len(label), len(value))
        paddedLabels.append(label.center(columnWidth))
        paddedValues.append(value.center(columnWidth))
    
    print ' '.join(paddedLabels)
    print ' '.join(paddedValues)
    

    Output:

    id Version chip_name  xversion   device  opt_param place_effort
    1    1.0    virtex2  xilinx11.5 xc5vlx50   Speed       High
    

    If you want to get fancy, make it reStructuredText-ready:

    labels = ['id', 'Version', 'chip_name', 'xversion', 'device', 'opt_param', 
        'place_effort']
    values = [1, 1.0, u'virtex2', u'xilinx11.5', u'xc5vlx50', u'Speed', u'High']
    
    paddedLabels = []
    paddedValues = []
    dividers = []
    dblDividers = []
    
    for label, value in zip(labels, values):
        value = str(value)
        columnWidth = max(len(label), len(value))
        paddedLabels.append(label.center(columnWidth))
        paddedValues.append(value.center(columnWidth))
        dividers.append('-' * columnWidth)
        dblDividers.append('=' * columnWidth)
    
    print '+-' + '-+-'.join(dividers) + '-+'
    print '| ' + ' | '.join(paddedLabels) + ' |'
    print '+=' + '=+='.join(dblDividers) + '=+'
    print '| ' + ' | '.join(paddedValues) + ' |'
    print '+-' + '-+-'.join(dividers) + '-+'
    

    Output:

    +----+---------+-----------+------------+----------+-----------+--------------+
    | id | Version | chip_name |  xversion  |  device  | opt_param | place_effort |
    +====+=========+===========+============+==========+===========+==============+
    | 1  |   1.0   |  virtex2  | xilinx11.5 | xc5vlx50 |   Speed   |     High     |
    +----+---------+-----------+------------+----------+-----------+--------------+