以彩色打印 PrettyTable

时间:2021-02-02 02:42:45

标签: python python-3.8 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', '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 |
+-------+-----+-----------+--------+---------+---------+

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)

因此我们避免意外地为表格边框着色。