forked from iliyahoo/Automate-The-Boring-Stuff-With-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintTable.py
More file actions
35 lines (30 loc) · 954 Bytes
/
printTable.py
File metadata and controls
35 lines (30 loc) · 954 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
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python3
tableData = [
['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose'],
]
def colWidth(tableData):
colWidths = [0] * len(tableData)
for col in range(len(tableData)):
for item in tableData[col]:
if len(item) > colWidths[col]:
colWidths[col] = len(item)
return colWidths
def pivot(tableData):
columns = []
for item in range(len(tableData[0])):
for col in range(len(tableData)):
try:
columns[item].append(tableData[col][item])
except:
columns.append([tableData[col][item]])
return columns
def print_table(tableData):
length = colWidth(tableData)
for row in pivot(tableData):
line = ''
for item in range(len(row)):
line += row[item].rjust(length[item] + 1)
print line
print_table(tableData)