python 如何将漂亮打印模块扩展到表格?

hgb9j2n6  于 2023-01-19  发布在  Python
关注(0)|答案(4)|浏览(142)

我准备了一个漂亮的打印模块,因为我不喜欢pprint模块生成了无数行的数字列表,而这些数字列表只有一个列表的列表。

>>> a=range(10)
    >>> a.insert(5,[range(i) for i in range(10)])
    >>> a
    [0, 1, 2, 3, 4, [[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4, 5, 6, 7], [0, 1, 2, 3, 4, 5, 6, 7, 8]], 5, 6, 7, 8, 9]
    >>> import pretty
    >>> pretty.ppr(a,indent=6)

    [0, 1, 2, 3, 4, 
          [
            [], 
            [0], 
            [0, 1], 
            [0, 1, 2], 
            [0, 1, 2, 3], 
            [0, 1, 2, 3, 4], 
            [0, 1, 2, 3, 4, 5], 
            [0, 1, 2, 3, 4, 5, 6], 
            [0, 1, 2, 3, 4, 5, 6, 7], 
            [0, 1, 2, 3, 4, 5, 6, 7, 8]], 5, 6, 7, 8, 9]

代码是这样的:

""" pretty.py prettyprint module version alpha 0.2
    mypr: pretty string function
    ppr:  print of the pretty string
    ONLY list and tuple prettying implemented!
"""
def mypr(w, i = 0, indent = 2, nl = '\n') :
    """ w = datastructure, i = indent level, indent = step size for indention """
    startend = {list : '[]', tuple : '()'}
    if type(w) in (list, tuple) :
        start, end = startend[type(w)]
        pr = [mypr(j, i + indent, indent, nl) for j in w]
        return nl + ' ' * i + start + ', '.join(pr) + end
    else :  return repr(w)

def ppr(w, i = 0, indent = 2, nl = '\n') :
    """ see mypr, this is only print of mypr with same parameters """
    print mypr(w, i, indent, nl)

下面是我的漂亮打印模块中一个用于表格打印的固定文本:

## let's do it "manually"
width = len(str(10+10))
widthformat = '%'+str(width)+'i'
for i in range(10):
    for j in range(10):
        print widthformat % (i+j),
    print

你有没有更好的替代方案,使这段代码足够通用,可以用于漂亮的打印模块?
我发现这种常见的情况下后,张贴的问题是这个模块:prettytable A simple Python library for easily displaying tabular data in a visually appealing ASCII table format

ctehm74n

ctehm74n1#

如果您希望为矩阵设置良好的格式,numpy的输出看起来非常棒:

from numpy import *
print array([[i + j for i in range(10)] for j in range(10)])

输出:

[[ 0  1  2  3  4  5  6  7  8  9]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 2  3  4  5  6  7  8  9 10 11]
 [ 3  4  5  6  7  8  9 10 11 12]
 [ 4  5  6  7  8  9 10 11 12 13]
 [ 5  6  7  8  9 10 11 12 13 14]
 [ 6  7  8  9 10 11 12 13 14 15]
 [ 7  8  9 10 11 12 13 14 15 16]
 [ 8  9 10 11 12 13 14 15 16 17]
 [ 9 10 11 12 13 14 15 16 17 18]]
jw5wzhpr

jw5wzhpr2#

你可以写:

'\n'.join(  # join the lines with '\n'
       ' '.join(  # join one line with ' '
              "%2d" % (i + j) # format each item
        for i in range(10))
    for j in range(10))
bvjxkvbb

bvjxkvbb3#

使用George Sakkis' table indention recipe

print(indent(((i + j for i in range(10)) for j in range(10)),
             delim=' ', justify='right'))

产量:

0  1  2  3  4  5  6  7  8  9
1  2  3  4  5  6  7  8  9 10
2  3  4  5  6  7  8  9 10 11
3  4  5  6  7  8  9 10 11 12
4  5  6  7  8  9 10 11 12 13
5  6  7  8  9 10 11 12 13 14
6  7  8  9 10 11 12 13 14 15
7  8  9 10 11 12 13 14 15 16
8  9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18

PS.为了让上面的工作正常进行,我对食谱做了一个小改动。我把wrapfunc(item)改成了wrapfunc(str(item))

def rowWrapper(row):
    newRows = [wrapfunc(str(item)).split('\n') for item in row]

相关问题