首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以彩色打印PrettyTable

以彩色打印PrettyTable
EN

Stack Overflow用户
提问于 2021-02-02 10:42:46
回答 2查看 496关注 0票数 0

是否有可能以彩色打印表格,例如带有HTML notation: 66a1d7和text: f09d52的框架?

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
+-------+-----+-----------+--------+---------+---------+
|  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 |
+-------+-----+-----------+--------+---------+---------+
EN

回答 2

Stack Overflow用户

发布于 2021-02-02 11:00:25

代码语言:javascript
复制
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)
票数 3
EN

Stack Overflow用户

发布于 2021-02-02 11:16:35

试一试

代码语言:javascript
复制
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)来实现,如下所示:

代码语言:javascript
复制
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)

所以我们避免意外地给表格边框上色。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66002953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档