返回列表 发帖

[原创代码] python3 打印表格

本帖最后由 Gin_Q 于 2020-8-22 14:55 编辑
'''
@ 从一个大列表里面格式化打印子列表
@ 2020/08/22
@ BY Cool_Breeze
'''
#coding=utf-8
class printf:
    def __init__(self, data, corner='+', head_tail='-', both_sides='|'):
        '''
        @ row_len 每一列数据最大长度的列表
        '''
        self.data = data
        self.corner = corner
        self.head_tail = head_tail
        self.both_sides = both_sides
        self.row_len = []
        self.__count_row_len()
    def __count_row_len(self):
        '''
        @ 计算每一列的长度
        @ 将每一个元素转换为字符串
        @ 按照第一个列表的长度来遍历所有列表,注意越界问题
        '''
        elements = len(self.data[0])
        for line_number in range(len(self.data)):
            if line_number == 0:
                for index in range(elements):
                    if not isinstance(self.data[line_number][index], str):
                        self.data[line_number][index] = str(self.data[line_number][index])
                self.row_len = [len(string) for string in self.data[line_number]]
            else:
                for index in range(elements):
                    if not isinstance(self.data[line_number][index], str):
                        self.data[line_number][index] = str(self.data[line_number][index])
                    if self.row_len[index] < len(self.data[line_number][index]):
                       self.row_len[index] = len(self.data[line_number][index])
    def split_line(self):
        '''
        @ 格式行
        @ 默认在每个元素两边各加上一个空格
        '''
        self.split_line_str = ''
        for i in self.row_len:
            self.split_line_str += (self.corner + self.head_tail * (i + 2))
        self.split_line_str += self.corner + '\n'
        print(self.split_line_str, end='')
        return self.split_line_str
    def data_line(self, title=True):
        '''
        @ 将字符串按格式整合
        @ 是否存在标题行
        @ title = True 将在第一行下面增加一行 split_line_str
        '''
        filling = ' '
        L_sides = self.both_sides + filling
        all_str = ''
        for elements in self.data:
            for nu,single in enumerate(elements):
                temp_single_len = len(single)
                if temp_single_len < self.row_len[nu]:
                    all_str += L_sides + single + filling * (self.row_len[nu] - temp_single_len + 1)
                else:
                    all_str += L_sides + single + filling
            all_str += self.both_sides + '\n'
            if title:
                all_str += self.split_line_str
                title = False
        print(all_str, end='')
        return all_str
if __name__ == '__main__':
    import csv
    test = []
    with open('test.csv') as f:
        f = csv.reader(f)
        for line in f:
            test.append(line)
    p = printf(test)
    # 写入日志
    with open('test.txt', 'w') as f:
        f.writelines(p.split_line())
        f.writelines(p.data_line())
        f.writelines(p.split_line())COPY
result:
+-----------+--------------+--------------+--------------+--------------+--------------+--------------+
| TimeStamp | TMON_0_RDIL0 | TMON_0_RDIL1 | TMON_0_RDIL2 | TMON_0_RDIL3 | TMON_0_RDIL4 | TMON_0_RDIL5 |
+-----------+--------------+--------------+--------------+--------------+--------------+--------------+
| 19:18.6   | 61           | 59.5         | 58.5         | 60.5         | 60.75        | 61.25        |
| 19:19.6   | 60.75        | 58.5         | 57.25        | 59.75        | 60           | 60.25        |
| 19:20.6   | 60.25        | 57.5         | 57           | 59.25        | 59.5         | 60.5         |
| 19:21.7   | 59.75        | 58           | 57.5         | 59.5         | 59.5         | 60.25        |
| 19:22.7   | 59.75        | 58           | 57.5         | 59.5         | 58.75        | 60           |
| 19:23.7   | 60           | 58           | 57.5         | 59.5         | 59.25        | 60.25        |
| 19:24.8   | 59.25        | 57.5         | 57.25        | 59           | 59           | 59.75        |
| 19:25.8   | 59           | 57.25        | 56.5         | 58.75        | 58.5         | 59.25        |
| 19:26.8   | 59.25        | 57.25        | 56.5         | 59           | 58.75        | 59.5         |
| 19:27.8   | 58.5         | 56           | 55.5         | 57.75        | 57.75        | 58.5         |
| 19:28.9   | 58.25        | 56           | 55.5         | 57.5         | 57.5         | 58.25        |
| 19:29.9   | 58.5         | 56.5         | 56           | 58           | 57.75        | 58.5         |
| 19:30.9   | 59.25        | 57.25        | 57           | 58.5         | 58.5         | 59.5         |
| 19:31.9   | 59           | 57.25        | 57           | 59.5         | 59.25        | 59.75        |
+-----------+--------------+--------------+--------------+--------------+--------------+--------------+
请按任意键继续. . .COPY

返回列表