gpt4 book ai didi

python - 漂亮的 Python 打印机?

转载 作者:行者123 更新时间:2023-11-28 19:40:59 28 4
gpt4 key购买 nike

我有一个标签列表,数据如下。

['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 7id      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'

这就是结果。

+----+---------+-----------+------------+----------+-----------+--------------+| 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     |+----+---------+-----------+------------+----------+-----------+--------------+

感谢您的帮助。

最佳答案

使用 ljust 在打印出来之前填充内容。

import sys

def maxwidth(table, index):
"""Get the maximum width of the given column index"""
return max([len(str(row[index])) for row in table])

def pprint_table(table):
colpad = []

for i in range(len(table[0])):
colpad.append(maxwidth(table, i))

for row in table:
print str(row[0]).ljust(colpad[0] + 1),
for i in range(1, len(row)):
col = str(row[i]).rjust(colpad[i] + 2)
print "", col,
print ""

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

# Put it in the table

c = [a, b]

pprint_table(c)

输出:

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

关于python - 漂亮的 Python 打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3697763/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com