I wrote a function for drawing a table for data that comes from file:
import data_manager
import os
table = data_manager.get_table_from_file('games.csv')
len_of_longest_name = 0
i = 0
while i < len(table):
if len_of_longest_name < len(table[i][1]):
len_of_longest_name = len(table[i][1])
i += 1
i = 0
len_of_longest_studio = 0
while i < len(table):
if len_of_longest_studio < len(table[i][2]):
len_of_longest_studio = len(table[i][2])
i += 1
print("/", 6 * "-", "|", (len_of_longest_name - 2) * "-", "|", (len_of_longest_studio - 2) * "-", "|", "---", "\\")
# constructing string
i = 0
while i < len(table):
string = '|'
string += table[i][0] + '|'
if len(table[i][1]) < len_of_longest_name:
spaces = (len_of_longest_name - len(table[i][1])) * ' '
string += table[i][1] + spaces + '|'
else:
string += table[i][1] + '|'
if len(table[i][2]) < len_of_longest_studio:
spaces = (len_of_longest_studio - len(table[i][2])) * ' '
string += table[i][2] + spaces + '|'
else:
string += table[i][2] + '|'
string += table[i][3] + '|'
if len(table[i][4]) < 2:
string += table[i][4] + ' |'
else:
string += table[i][4] + '|'
if i < len(table) - 1:
print(string)
print("|", 6 * "-", "|", (len_of_longest_name - 2) * "-", "|", (len_of_longest_studio - 2) * "-", "|", "---", "|")
else:
print(string)
i += 1
print("\\", 6 * "-", "|", (len_of_longest_name - 2) * "-", "|", (len_of_longest_studio - 2) * "-", "|", "---", "/")
The code works as I want but I think there's a better (shorter) method to do the same. Could you show me the method?
The output looks like this:
data_manager
? \$\endgroup\$