- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个标签列表,数据如下。
['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?
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/
在通过REST Request Body方法发出搜索请求时,例如 GET /bank/_search { "query": { "match_all": {} }, "sort": [
有没有办法检测用户是否停止滑动?也许 bij 鼠标输入检查之类的。我尝试在 onSliderChange 事件中使用鼠标 isButton0Release 和 hasFocus 来检查用户是否停止滑动
(来源:google.com) 最近,我发现我的应用程序中显示的汉字相当难看。 我想我应该让它们“消除锯齿”。但是,我如何在 Java 中做到这一点? 仅供引用,我没有明确选择要在 GUI 应用程序中
我正在创建一个网站,根据某些条件(即是否登录、是否填写表格等),不同页面的外观可能会有很大差异。这使得需要在不同的时间输出不同的html block 。 但是,这样做会使我的 php 代码看起来很糟糕
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
如果我有一个类包含例如几个将用向量填充的槽,那么问题通常会出现。如果我想让这个类的对象或多或少透明,我会为它实现 print-object。在这里我遇到了问题: 如果我在一行中打印所有内容,REPL
我有一个允许上传到我网站的文件扩展名列表。 我用 jQuery Validation plugin 检查它们. 如果他们选择了不受支持的扩展程序,我会显示一条错误消息。 看起来像 var msg =
在一个本身嵌套在一个或多个父数组/结构/union 中的数组/结构/union 中读取多个数字/字符串的最佳方法是什么? 没有临时变量的第一个例子: printf("%d %d\n", a[9][3]
我是 AngularJS 的新手。我目前在为我的观点编写漂亮的 url 时遇到问题。我已经设置了 $locationProvider.html5Mode(true);删除 #从地址栏中显示的网址。但是
我有一个带有 ElasticSearch 的自动完成 jquery 功能。输入第一个字母时出现以下错误。 跨源请求被阻止:同源策略不允许读取 http://localhost:9200/test12/
我是一名优秀的程序员,十分优秀!