Reputation: 81
Would it be possible to print the table in color, for example frames with HTML notation: 66a1d7 and text: 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 |
+-------+-----+-----------+--------+---------+---------+
Upvotes: 0
Views: 3757
Reputation: 1449
Try
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
Response for comment
in order to make coloring easier, we can make it via a function item_painting(item, color)
as below:
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)
so we avoid coloring accidentally the table borders.
Upvotes: 1
Reputation: 2255
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)
Upvotes: 3