python 显示ASCII表中的行和列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 显示ASCII表中的行和列相关的知识,希望对你有一定的参考价值。

# /usr/bin/env python
# -*- coding: utf-8 -*-

"""
sq.texttable
~~~~~~~~~~~~

Creates simple ASCII tables
Based on texttables by Gerome Fournier
..'https://pypi.python.org/pypi/texttable/0.8.1'_

TextTable features not yet implemented:
1. Horizontal Alignment within cell (left, center, right).
   Currently set to left.
2. Vertical Alignment within cell (top, middle, bottom).
   Currently set to top.
3. Set column width.
   Currently set to auto.
4. Set precision on floats & exponential values
5. Values must be strings
6. Borders are a place holder for future development, not currently implemented

"""


class Border:
    """
    Line 1:  +===========+===========+
    Line 2:  | HEADER1   | HEADER2   |
    Line 3:  +===========+===========+
    Line 4:  | ROW1-COL1 | ROW1-COL2 |
    Line 5:  +-----------+-----------+
    Line 6:  | ROW2-COL1 | ROW2-COL2 |
    Line 7:  +===========+===========+

    LINE1: CORNER + TOP_BOTTOM_BORDER*11 + TOP_BOTTOM_JOIN
           + TOP_BOTTOM_BORDER*11 + CORNER

    LINE2: LEFT_RIGHT_BORDER + ' '*PADDING + 'HEADER1' + ' '*PADDING
           + COLUMN_SEPERATOR + ' '*PADDING + 'HEADER2' + ' '*PADDING
           + LEFT_RIGHT_BORDER

    LINE3: RIGHT_LEFT_JOIN + BORDER_HEADER*11 + INNER_JOIN +
            HEADER_SEPARATOR*11 + RIGHT_LEFT_JOIN

    LINE4: LEFT_RIGHT_BORDER + ' '*PADDING + 'ROW1-COL1' + ' '*PADDING
           + COL_SEPERATOR  + ' '*PADDING + 'ROW1-COL2' + ' '*PADDING
           + LEFT_RIGHT_BORDER

    LINE5: RIGHT_LEFT_JOIN + ROW_SEPERATOR*11 + INNER_JOIN +
            ROW_SEPERATOR*11 + RIGHT_LEFT_JOIN

    LINE 6: Similar to line 4
    LINE 7: Same as line 1
    """

    # Border characters
    TOP_BOTTOM_BORDER, LEFT_RIGHT_BORDER = '_', '|'

    # Seperator characters
    HEADER_SEPARATOR, ROW_SEPERATOR, COLUMN_SEPARATOR = '=', '-', '|'

    # Join (intesection) characters
    INNER_JOIN, TOP_BOTTOM_JOIN, RIGHT_LEFT_JOIN = '+', '+', '+'
    CORNER = '+'


class TextTable:

    def __init__(self, table_data=None, has_header=None, has_border=None,
                 row_separator='_', column_separator='|'):
        self.rows = []
        self.has_header = has_header
        self.borders = Border() if has_border else None
        self.row_separator = row_separator
        self.column_seperator = column_separator
        if table_data:
            self.add_rows(table_data)
        self.padding = ' '

    def draw(self, rows):
        cols = list(zip(*rows))
        for colnum, col in enumerate(cols):
            width = max(len(i) for i in col)
            cols[colnum] = [i.ljust(width) for i in col]
        for row in zip(*cols):
            print(self.padding.join(row))


def texttable(matrix):
    t = TextTable()
    t.draw(matrix)

以上是关于python 显示ASCII表中的行和列的主要内容,如果未能解决你的问题,请参考以下文章

选择 QTableWidget 中的行和列,同时保持突出显示

使用 pandas-Python 3 遍历 excel 中的行和列

怎么显示Oracle数据库表中的列

C#中定义了一个DataTable,怎样求得表中的最大值,并返回对应的行和列的下标啊

查找在用户窗体中键入的值的行和列

如何在 MVC 应用程序中转置 Kendo UI 网格中的行和列?