如何将漂亮的打印模块扩展到表格? [英] How to extend pretty print module to tables?

查看:149
本文介绍了如何将漂亮的打印模块扩展到表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有漂亮的打印模块,这是我准备的,因为我不乐意pprint模块为列表中的数字列表创建了数十亿行。这里是我的模块的例子。

 >>> a =范围(10)
>>> a.insert(5,[range(i)for i in range(10)])
>>>一个
[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] b $ b>>>导入漂亮的
>>> 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]



c $ c>pretty.py prettyprint模块版本alpha 0.2
mypr:漂亮的字符串函数
ppr:可爱的字符串打印
只有列表和元组实现!

def mypr(w,i = 0,indent = 2,nl ='\ n'):
w =数据结构,i =缩进级别,indent =步长(w)in(list,tuple):
start,end = startend()
startend = {list:'[]',tuple:' [类型(w)]
pr = [m ypr(j,i + indent,indent,nl)for j in w]
return nl +''* i + start +','.join(pr)+ end
else:return repr w)
$ b $ def ppr(w,i = 0,indent = 2,nl ='\\\
'):
见mypr,这只是mypr的打印参数
print mypr(w,i,indent,nl)

一个固定的文本在我漂亮的打印模块中进行表格打印:

  ##让我们手动执行
width = (10):
widthformat ='%'+ str(width)+'i'
(10) :
print widthformat%(i + j),
print

这个代码的更好的替代方案被推广到漂亮的打印模块吗?



在发布这个问题后,我发现这种常见的情况是这个模块: prettytable一个简单的Pyt用于以一种视觉上吸引人的ASCII表格格式容易地显示表格数据

解决方案

我对这种常规例子是使用这个模块:
prettytable
一个简单的Python库以一种视觉上吸引人的ASCII表格格式显示表格数据


I have the pretty print module, which I prepared because I was not happy the pprint module produced zillion lines for list of numbers which had one list of list. Here is example use of my module.

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

Code is like this:

""" 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)

Here is one fixed text for table printing in my pretty print module:

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

Have you better alternative for this code to be generalized enough for the pretty printing module?

What I found for this kind of regular cases after posting the question is this module: prettytable A simple Python library for easily displaying tabular data in a visually appealing ASCII table format

解决方案

My answer to this kind of regular cases would be to use this module: prettytable A simple Python library for easily displaying tabular data in a visually appealing ASCII table format

这篇关于如何将漂亮的打印模块扩展到表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆