如何解决以彩色打印 PrettyTable
是否可以用彩色打印表格,例如带有 HTML 符号的框架:66a1d7 和文本:f09d52?
from prettytable import PrettyTable
people = {1: {'name': 'John','age': '27','city': 'London','sex': 'Male','married': 'Yes','phoneNo': '000001'},2: {'name': 'Marie','age': '22','sex': 'Female','married': 'No','phoneNo': '000002'},3: {'name': 'Luna','age': '24','city': 'Edinburgh','phoneNo': '000003'},4: {'name': 'Peter','age': '29','phoneNo': '000004'}}
mytable= PrettyTable(['Name','Age','City','Sex','Marital','PhoneNo'])
for x in people:
lis=[ x for x in people]
li = [y for x,y in people[x].items()]
mytable.add_row(li)
print(mytable)
输出:
+-------+-----+-----------+--------+---------+---------+
| Name | Age | City | Sex | Marital | PhoneNo |
+-------+-----+-----------+--------+---------+---------+
| John | 27 | London | Male | Yes | 000001 |
| Marie | 22 | London | Female | No | 000002 |
| Luna | 24 | Edinburgh | Female | No | 000003 |
| Peter | 29 | Edinburgh | Male | Yes | 000004 |
+-------+-----+-----------+--------+---------+---------+
解决方法
from prettytable import PrettyTable
class ConsoleColor:
# Color
BLACK = '\033[90m'
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
PURPLE = '\033[95m'
CYAN = '\033[96m'
GRAY = '\033[97m'
# Style
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# BackgroundColor
BgBLACK = '\033[40m'
BgRED = '\033[41m'
BgGREEN = '\033[42m'
BgORANGE = '\033[43m'
BgBLUE = '\033[44m'
BgPURPLE = '\033[45m'
BgCYAN = '\033[46m'
BgGRAY = '\033[47m'
# End
END = '\033[0m'
people = {1: {'name': 'John','age': '27','city': 'London','sex': 'Male','married': 'Yes','phoneNo': '000001'},2: {'name': 'Marie','age': '22','sex': 'Female','married': 'No','phoneNo': '000002'},3: {'name': 'Luna','age': '24','city': 'Edinburgh','phoneNo': '000003'},4: {'name': 'Peter','age': '29','phoneNo': '000004'}}
mytable = PrettyTable(['Name','Age','City','Sex','Marital','PhoneNo'])
for x in people:
lis = [x for x in people]
li = [y for x,y in people[x].items()]
li[1] = ConsoleColor.GREEN + li[1] + ConsoleColor.END
mytable.add_row(li)
print(mytable)
,
试试
mytable= PrettyTable(['Name','PhoneNo'])
for x in people:
people[x]['age'] = '\u001b[33m' + people[x]['age'] + '\u001b[0m'
li = [y for x,y in people[x].items()]
mytable.add_row(li)
print(mytable)
https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
评论回复
为了使着色更容易,我们可以通过函数item_painting(item,color)
来实现,如下所示:
def item_painting(item,color):
# 'black','red','green','yellow','blue','magenta','cyan','white','reset'
color_code = {'black': '\u001b[30m','red': '\u001b[31m','green': '\u001b[32m','yellow': '\u001b[33m','blue': '\u001b[34m','magenta': '\u001b[35m','cyan': '\u001b[36m','white': '\u001b[37m','reset': '\u001b[0m'}
return f'{color_code[color.lower()]}{item}\u001b[0m'
for x in people:
# people[x]['age'] = '\u001b[33m' + people[x]['age'] + '\u001b[0m'
people[x]['age'] = item_painting(people[x]['age'],'yellow')
li = [y for x,y in people[x].items()]
mytable.add_row(li)
print(mytable)
因此我们避免意外地为表格边框着色。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。