安装:pip install pylsy

from __future__ import print_function
from wcwidth import wcwidthimport timeclass pylsytable(object):def __init__(self, attributes):"""Creates a new PylsyTable object with the given attrs (cols)."""self.StrTable = ""self.Table = []self.AttributesLength = []self.Lines_num = 0if type(attributes) != list:attributes = [attributes]self.Attributes = [u"{0}".format(attr) for attr in attributes]self.Cols_num = len(self.Attributes)for attribute in self.Attributes:col = dict()col[attribute] = []self.Table.append(col)def _print_divide(self):"""Prints all those table line dividers."""for space in self.AttributesLength:self.StrTable += "+ " + "- " * spaceself.StrTable += "+"+"\n"def append_data(self, attribute, values):"""Appends the given value(s) to the attribute (column)."""found = Falseif type(values) != list:values = [values]for col in self.Table:if attribute in col:dict_values = [u"{0}".format(value) for value in values]col[attribute] += dict_valuesfound = Trueif not found:raise KeyError(attribute)def add_data(self, attribute, values):"""Sets the given values for the attribute (column)."""found = Falseif type(values) != list:values = [values]for col in self.Table:if attribute in col:dict_values = [u"{0}".format(value) for value in values]col[attribute] = dict_valuesfound = Trueif not found:raise KeyError(attribute)def _create_table(self):"""Creates a pretty-printed string representation of the table as``self.StrTable``."""self.StrTable = ""self.AttributesLength = []self.Lines_num = 0# Prepare some values..for col in self.Table:# Updates the table line count if necessaryvalues = list(col.values())[0]self.Lines_num = max(self.Lines_num, len(values))# find the length of longest value in current columnkey_length = max([self._disp_width(v) for v in values] or [0])# and also the table headerkey_length = max(key_length, self._disp_width(list(col.keys())[0]))self.AttributesLength.append(key_length)# Do the real thing.self._print_head()self._print_value()def _print_head(self):"""Generates the table header."""self._print_divide()self.StrTable += "| "for colwidth, attr in zip(self.AttributesLength, self.Attributes):self.StrTable += self._pad_string(attr, colwidth * 2)self.StrTable += "| "self.StrTable += '\n'self._print_divide()def _print_value(self):"""Generates the table values."""for line in range(self.Lines_num):for col, length in zip(self.Table, self.AttributesLength):vals = list(col.values())[0]val = vals[line] if len(vals) != 0 and line < len(vals) else ''self.StrTable += "| "self.StrTable += self._pad_string(val, length * 2)self.StrTable += "|"+'\n'self._print_divide()def _disp_width(self, pwcs, n=None):"""A wcswidth that never gives -1. Copying existing code is evil, but..github.com/jquast/wcwidth/blob/07cea7f/wcwidth/wcwidth.py#L182-L204"""# pylint: disable=C0103#         Invalid argument name "n"# TODO: Shall we consider things like ANSI escape seqs here?#       We can implement some ignore-me segment like those wrapped by#       \1 and \2 in readline too.end = len(pwcs) if n is None else nidx = slice(0, end)width = 0for char in pwcs[idx]:width += max(0, wcwidth(char))return widthdef _pad_string(self, str, colwidth):"""Center-pads a string to the given column width using spaces."""width = self._disp_width(str)prefix = (colwidth - 1 - width) // 2suffix = colwidth - prefix - widthreturn ' ' * prefix + str + ' ' * suffixdef __str__(self):"""Returns a pretty-printed string representation of the table."""self._create_table()return self.StrTable# 使用
attributes=["name","age","sex","id","time"]
table=pylsytable(attributes)
name=["sun"]
table.add_data("name",name)
#table.append_data("name",["leviathan"])
#table.append_data("name",u"小明")for i in range(15):time.sleep(0.5)id=[str(i)]table.add_data("id",id)print("\r{}".format(table), end='',  flush=True)

python 终端打印表格--------pylsy 源码相关推荐

  1. python终端打印表格

    1 python终端打印表格 使用PrettyTable库可以较为简单的完成一个终端表格的打印,在引用PrettyTable库后具体使用的关键代码,可以总结为如下五行: x = PrettyTable ...

  2. python五子棋小游戏程序源码

    wx供重浩:创享日记 对话框发送:python五子棋 免费获取完整源码源文件+配置教程文件+可执行文件等 在PyCharm中运行<五子棋(控制台版)>即可进入如图1所示的系统主界面. 具体 ...

  3. 票据打印, 银行账单打印, 标签印刷, 文档打印, 条码打印, 批量打印, 包装纸打印与设计,可变数据打印,数据库印刷,HMI报表打印,VC++源码库解决方案

    票据打印, 银行账单打印, 标签印刷, 文档打印, 条码打印, 批量打印, 包装纸打印与设计,可变数据打印,数据库印刷,HMI报表打印,VC++源码库解决方案 -- 全球领先.100%源码全开放.可用 ...

  4. 开源Vue表格组件,表格插件源码

    开源Vue表格组件,表格插件源码 前言: 关于html里面原生的table,通常满足不了程序员的要求.所以开发了一款表格插件,其功 能有: 1 导入json格式数据后,自动填充表格.表格长宽自适应.排 ...

  5. python设计模式pdf_精通Python设计模式 高清晰PDF+源码

    精通Python设计模式讲述了16种基本设计模式,轻松解决软件设计常见问题:借力高效的Python语言,用现实例子展示各模式关键特性. 本书用实际生活中的例子带你了解常用的设计模式,介绍了诸多有关编写 ...

  6. Go+Vue打印店远程打印小程序源码

    简介: Go+Vue打印店远程打印小程序源码,带微信小程序前端代码和后台程序源码,用户可以通过小程序上传文件→店主在线预览(下载)&打印. 网盘下载地址: http://kekewl.cc/j ...

  7. cvtcolor python opencv_13行代码实现:Python实时视频采集(附源码)

    程序逻辑 Python实时视频采集程序主要流程共分为10个步骤,具体如下图所示: 流程描述: 库文件导入:导入程序依赖的python安装包: 摄像头管理对象创建和初始化:是对opencv VideoC ...

  8. 【Python 笔记】Linux 下源码编译安装 python

    本文记录在 Linux 上源码编译安装 python 的过程. 文章目录 1. 源码编译安装说明 2. 安装 python2.7 3. 安装 python3.6 1. 源码编译安装说明 安装过程比我想 ...

  9. 二维码条形码生成打印软件C#源码,根据变量自动添加抬头

    二维码条形码生成打印软件C#源码,根据变量自动添加抬头,非常方便,工控朋友可以直接拿过去使用,完整源码 16100635774317133鲲鹏自动化

最新文章

  1. [BUUCTF-pwn]——inndy_rop
  2. mysql百万级去重_mysql优化小技巧之去除重复项(百万级数据)
  3. C++中类成员变量在初始化列表中的初始化顺序
  4. Mac jupyter 绘制matplotlib图像解决中文空格的问题
  5. html快照抓取,请教前端实现获取dom元素快照的方法
  6. Linux运维系统工程师系列---17
  7. idea引入外部jar,运行项目不报错,使用maven打war包install/package时报程序包不存在等
  8. 【Linux】虚拟服务器之LVS
  9. 吃透这套架构演化图,从零搭建Web网站也不难!
  10. 多功能小游戏神器微信小程序源码
  11. oracle库怎么样查询gp数据库,GP数据库分布键查询
  12. 腐蚀rust服务器命令_腐蚀rust服务器命令一览 腐蚀rust有哪些命令
  13. 中国传统四大菜系之:鲁菜
  14. 切换无线网卡失败服务器提示,电脑无线网卡切换为AP模式时提示ICS启动失败的解决方法...
  15. 初识JavaScript,体验JS的美好
  16. SSM整合(搭建一个Web脚手架)
  17. idea打开后不显示界面,win+D快捷键解决问题
  18. C语言:银行储蓄系统开发(中级)
  19. springboot停车场车辆定位管理可视化分析系统 毕业设计-附源码101702
  20. 【菜鸟窝出品】 python的变量和逻辑基础(python数据分析入门)

热门文章

  1. 重复制造业之成本核算
  2. 浅谈ATP检查中的“确认可用部分数量”(二)
  3. Dynpro程序抬头信息要求多值输入的解决方法
  4. SAP LSMW批导数据的几个注意点
  5. SVN和Git的比较
  6. 选择头秃还是植发?大数据告诉你植发行业水有多深?
  7. 与年轻人“玩在一起”的QQ音乐,正抢跑音娱赛道?
  8. 陌陌财报双双超预期,什么才是它的基本盘?
  9. python竞赛_浅谈Python在信息学竞赛中的运用及Python的基本用法
  10. set python_python基础:集合-set()