forked from chenguohui/AutomatePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintTable.py
More file actions
25 lines (19 loc) · 726 Bytes
/
printTable.py
File metadata and controls
25 lines (19 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def getColWidth(table, widths):
for i in range(len(table)):
colWidth = 0
for item in table[i]:
widths[i] = max(widths[i], len(item))
def printTable(table, widths):
rowNum = len(table)
colNum = len(table[0])
for y in range(colNum):
print(table[0][y].ljust(widths[0]), end=' ')
for x in range(1, rowNum):
print(table[x][y].ljust(widths[x]), end=' ')
print()
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
colWidths = [0] * len(tableData)
getColWidth(tableData, colWidths)
printTable(tableData, colWidths)