用于 Python 的文本表编写器/打印机 [英] A Text Table Writer/Printer for Python

查看:48
本文介绍了用于 Python 的文本表编写器/打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

TL;DR -> PyPi 上是否有一个表格编写模块(我没有找到任何一个),它将列表作为参数并从这些列表中制作一个表格.我问这个是因为我看过 PyPI,但我没有发现任何类似于实际打印字符串或将字符串写入文件的内容.

想象一下有很多统计数据,并且必须像这样将它们整齐地写在表格中,(我一直试图教一门关于不同排序算法之间差异的课程)(另外,请注意此处给出的示例与下面给出的代码的输出不匹配.我简单地这样做是为了解释我想要什么,而不是编写大量必须滚动的代码):

######################### 长度 |||时间########################### 0 |||0.00000 ## 250 |||0.00600## 500 |||0.02100## 750 |||0.04999## 1000 |||0.08699#第 1250 章0.13499## 1500 |||0.19599#第 1750 章0.26900## 2000 |||0.35099##########################

理想情况下,我会写这样的东西来保存到文件中,如下所示.一组列表,一个列表包含一组值,另一个包含另一组对应值.

如果 __name__ == '__main__':以 open(os.path.join(os.path.dirname(__file__), 'Sort Stats', 'stats_exp.txt'), 'w') 作为统计信息:stats.write("O-#####################==#####################==######################==####################-上")stats.writelines("|{0:^23}||{1:^23}||{2:^23}||{3:^23}|\n".format("冒泡排序", "插入排序", "归并排序 (R)",合并排序(I)"))stats.write("|######################||######################||######################||######################|\n")stats.write("| LENGTH | TIME(s) || LENGTH | TIME(s) || LENGTH | TIME(s) || LENGTH | TIME(s) |\n")stats.write("|######################||######################||######################||######################|\n")对于 zip 中的 times_taken、t1、t2、t3、t4(total_lengths、sort_times_bubble、sort_times_ins、sort_times_merge_r、sort_times_merge_i):stats.write("|{0:^11}|{1:^11}||{2:^11}|{3:^11}||{4:^11}|{5:^11}||{6:^11}|{7:^11}|\n".格式(times_taken, str(t1)[:6],times_taken, str(t2)[:6],times_taken, str(t3)[:6],times_taken, str(t4)[:6],))stats.write("O-#####################==#####################==######################==####################-上")打印数据写入完成"

如您所见,它并不完全漂亮,最重要的是它不是易于扩展的东西,因为它几乎可以打印出一些东西.

我想做的是为此创建一个模块并将其上传到 PyPI.但是,如果有人已经做出了类似的东西,那将是白费力气,我可以简单地 fork 他们的存储库,而不必从头开始编写代码.

解决方案

PrettyTable 模块是什么你需要:

<块引用>

PrettyTable 是一个简单的 Python 库,旨在使其快速且易于在视觉上吸引人的 ASCII 表中表示表格数据.

<预><代码>>>>导入漂亮的>>>x = Prettytable.PrettyTable(["长度", "时间"])>>>x.add_row([0, 0.00000])>>>x.add_row([250, 0.00600])>>>x.add_row([500, 0.02100])>>>x.add_row([750, 0.04999])>>>打印 x+--------+---------+|长度 |时间 |+--------+---------+|0 |0.0 ||250 |0.006 ||500 |0.021 ||第 750 章0.04999 |+--------+---------+

或者,texttable:

<块引用>

texttable 是一个生成格式化文本表的模块,使用 ASCII字符.

<预><代码>>>>导入文本表>>>x = texttable.Texttable()>>>x.add_rows([["Length", "Time"], [0, 0.00000], [250, 0.00600], [500, 0.02100], [750, 0.04999]])>>>打印 x.draw()+--------+-------+|长度 |时间 |+========+========+|0 |0 |+--------+-------+|250 |0.006 |+--------+-------+|500 |0.021 |+--------+-------+|750 |0.050 |+--------+-------+

另请参阅相关主题:如何使用 Python 漂亮地打印 ASCII 表?

TL;DR -> Is there a table writing module on PyPi (I've failed to find any) that takes in lists as parameters and makes a table out of those lists. I am asking this because I've looked on PyPI, but I have not found anything similar to actually printing strings or writing strings to files.

Imagine having a lot of statistics, and having to write them down neatly in a table, like this, (I've been trying to teach a class about the differences between the different sorting algorithms out there) (Also, please note that the example given here does not match the output of the code given below. I've simple done this in order to explain what I want and not make huge chunks of code that one has to scroll through):

#########################
#   LENGTH ||| TIME(s)  #
#########################
#       0  ||| 0.00000  #
#     250  ||| 0.00600  #
#     500  ||| 0.02100  #
#     750  ||| 0.04999  #
#    1000  ||| 0.08699  #
#    1250  ||| 0.13499  #
#    1500  ||| 0.19599  #
#    1750  ||| 0.26900  #
#    2000  ||| 0.35099  #
#########################

Ideally, I would write something like this to save to a file, like the one below. set of lists, one list containing the one set of values, the other containing another set of corresponding values.

if __name__ == '__main__':
    with open(os.path.join(os.path.dirname(__file__), 'Sort Stats', 'stats_exp.txt'), 'w') as stats:
        stats.write(
            "O-######################==#######################==#######################==######################-O\n")
        stats.writelines(
            "|{0:^23}||{1:^23}||{2:^23}||{3:^23}|\n".format("Bubble Sort", "Insertion Sort", "Merge Sort (R)",
                                                            "Merge Sort (I)"))
        stats.write(
            "|#######################||#######################||#######################||#######################|\n")
        stats.write(
            "|   LENGTH  |  TIME(s)  ||   LENGTH  |  TIME(s)  ||   LENGTH  |  TIME(s)  ||   LENGTH  |  TIME(s)  |\n")
        stats.write(
            "|#######################||#######################||#######################||#######################|\n")
        for times_taken, t1, t2, t3, t4 in zip(total_lengths, sort_times_bubble, sort_times_ins, sort_times_merge_r,
                                               sort_times_merge_i):
            stats.write(
                "|{0:^11}|{1:^11}||{2:^11}|{3:^11}||{4:^11}|{5:^11}||{6:^11}|{7:^11}|\n"
                .format(
                    times_taken, str(t1)[:6],
                    times_taken, str(t2)[:6],
                    times_taken, str(t3)[:6],
                    times_taken, str(t4)[:6],
                )
            )
        stats.write(
            "O-######################==#######################==#######################==######################-O\n")

    print "Data writing complete"

As you can see, its not exactly pretty, and most importantly its not something that is easy to extend, since it pretty much prints something out.

What I want to do is create a module for this and upload it to PyPI. However, if someone's already made something akin to this, then it would be wasted effort, and I could simple fork their repositories instead of actually having to write the code from scratch.

解决方案

PrettyTable module is what you need:

PrettyTable is a simple Python library designed to make it quick and easy to represent tabular data in visually appealing ASCII tables.

>>> import prettytable
>>> x = prettytable.PrettyTable(["Length", "Time"])
>>> x.add_row([0, 0.00000])
>>> x.add_row([250, 0.00600]) 
>>> x.add_row([500, 0.02100]) 
>>> x.add_row([750, 0.04999])    
>>> print x
+--------+---------+
| Length |   Time  |
+--------+---------+
|   0    |   0.0   |
|  250   |  0.006  |
|  500   |  0.021  |
|  750   | 0.04999 |
+--------+---------+

Or, texttable:

texttable is a module to generate a formatted text table, using ASCII characters.

>>> import texttable
>>> x = texttable.Texttable()
>>> x.add_rows([["Length", "Time"], [0, 0.00000], [250, 0.00600], [500, 0.02100], [750, 0.04999]])
>>> print x.draw()
+--------+-------+
| Length | Time  |
+========+=======+
| 0      | 0     |
+--------+-------+
| 250    | 0.006 |
+--------+-------+
| 500    | 0.021 |
+--------+-------+
| 750    | 0.050 |
+--------+-------+

Also see relevant thread: How can I pretty-print ASCII tables with Python?

这篇关于用于 Python 的文本表编写器/打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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