python 表 输出 ascii_如何用Python漂亮地打印ASCII表?

这里有一个快速而肮脏的小函数,我编写它来显示来自SQL查询的结果,我只能通过SOAP API来实现。它要求输入一个或多个namedtuples序列作为表行。如果只有一个记录,它会以不同的方式打印出来。

它对我来说很方便,可能是你的起点:def pprinttable(rows):

if len(rows) > 1:

headers = rows[0]._fields

lens = []

for i in range(len(rows[0])):

lens.append(len(max([x[i] for x in rows] + [headers[i]],key=lambda x:len(str(x)))))

formats = []

hformats = []

for i in range(len(rows[0])):

if isinstance(rows[0][i], int):

formats.append("%%%dd" % lens[i])

else:

formats.append("%%-%ds" % lens[i])

hformats.append("%%-%ds" % lens[i])

pattern = " | ".join(formats)

hpattern = " | ".join(hformats)

separator = "-+-".join(['-' * n for n in lens])

print hpattern % tuple(headers)

print separator

_u = lambda t: t.decode('UTF-8', 'replace') if isinstance(t, str) else t

for line in rows:

print pattern % tuple(_u(t) for t in line)

elif len(rows) == 1:

row = rows[0]

hwidth = len(max(row._fields,key=lambda x: len(x)))

for i in range(len(row)):

print "%*s = %s" % (hwidth,row._fields[i],row[i])

样本输出:pkid | fkn | npi

-------------------------------------+--------------------------------------+----

405fd665-0a2f-4f69-7320-be01201752ec | 8c9949b9-552e-e448-64e2-74292834c73e | 0

5b517507-2a42-ad2e-98dc-8c9ac6152afa | f972bee7-f5a4-8532-c4e5-2e82897b10f6 | 0

2f960dfc-b67a-26be-d1b3-9b105535e0a8 | ec3e1058-8840-c9f2-3b25-2488f8b3a8af | 1

c71b28a3-5299-7f4d-f27a-7ad8aeadafe0 | 72d25703-4735-310b-2e06-ff76af1e45ed | 0

3b0a5021-a52b-9ba0-1439-d5aafcf348e7 | d81bb78a-d984-e957-034d-87434acb4e97 | 1

96c36bb7-c4f4-2787-ada8-4aadc17d1123 | c171fe85-33e2-6481-0791-2922267e8777 | 1

95d0f85f-71da-bb9a-2d80-fe27f7c02fe2 | 226f964c-028d-d6de-bf6c-688d2908c5ae | 1

132aa774-42e5-3d3f-498b-50b44a89d401 | 44e31f89-d089-8afc-f4b1-ada051c01474 | 1

ff91641a-5802-be02-bece-79bca993fdbc | 33d8294a-053d-6ab4-94d4-890b47fcf70d | 1

f3196e15-5b61-e92d-e717-f00ed93fe8ae | 62fa4566-5ca2-4a36-f872-4d00f7abadcf | 1

示例>>> from collections import namedtuple

>>> Row = namedtuple('Row',['first','second','third'])

>>> data = Row(1,2,3)

>>> data

Row(first=1, second=2, third=3)

>>> pprinttable([data])

first = 1

second = 2

third = 3

>>> pprinttable([data,data])

first | second | third

------+--------+------

1 | 2 | 3

1 | 2 | 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值