以彩色打印 PrettyTable
Posted
技术标签:
【中文标题】以彩色打印 PrettyTable【英文标题】:Print the PrettyTable in color 【发布时间】:2021-05-06 05:37:48 【问题描述】:是否可以用彩色打印表格,例如带有 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', 'city': 'London', 'sex': 'Female', 'married': 'No', 'phoneNo': '000002',
3: 'name': 'Luna', 'age': '24', 'city': 'Edinburgh', 'sex': 'Female', 'married': 'No', 'phoneNo': '000003',
4: 'name': 'Peter', 'age': '29', 'city': 'Edinburgh', 'sex': 'Male', 'married': 'Yes', '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 |
+-------+-----+-----------+--------+---------+---------+
【问题讨论】:
一个 hacky 解决方案是手动迭代源字典并在每个单词之前和之后添加 ASCII 颜色代码和颜色重置代码 【参考方案1】:试试
mytable= PrettyTable(['Name', 'Age', 'City', 'Sex', 'Marital', '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)
所以我们避免意外着色表格边框。
【讨论】:
偶然我完成了 mytable= PrettyTable(['\u001b[36m' +'Name', 'Age', 'City', 'Sex', 'Marital', 'PhoneNo' ] ) 并忘记在末尾添加:+'\u001b[0m'。这样做的效果是文本和框架边框都是彩色的,所以可以为边框着色,但我不能为整个表格制作。【参考方案2】: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', 'city': 'London', 'sex': 'Female', 'married': 'No', 'phoneNo': '000002',
3: 'name': 'Luna', 'age': '24', 'city': 'Edinburgh', 'sex': 'Female', 'married': 'No', 'phoneNo': '000003',
4: 'name': 'Peter', 'age': '29', 'city': 'Edinburgh', 'sex': 'Male', 'married': 'Yes', '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)
【讨论】:
以上是关于以彩色打印 PrettyTable的主要内容,如果未能解决你的问题,请参考以下文章