Ответ:
def print_pretty_table(data, cell_sep=' | ', header_separator=True):
rows = len(data)
cols = len(data[0])
col_width = []
for col in range(cols):
columns = [data[row][col] for row in range(rows)]
col_width.append(len(max(columns, key=len)))
separator = "-+-".join('-' * n for n in col_width)
for i, row in enumerate(range(rows)):
if i == 1 and header_separator:
print(separator)
result = []
item = data[row][col].rjust(col_width[col])
result.append(item)
print(cell_sep.join(result))
Добавляем в Customer метод:
def values(self):
return self.get_name(), self.get_address(), self.get_telephone(), \
self.get_email(), self.mailList(),self.get_number()
И итог:
def main():
customer = Customer('Josh', 'Long st, Dallas TX', '555-987-1549','[email protected]','Y','1153')
customer1 = Customer('Djon', 'Long st, Dallas TX', '555-987-1549','[email protected]','Y','1153')
customer2 = Customer('Ser', 'Long st, Dallas TX', '555-987-1549','[email protected]','Y','1153')
table_data = [
['NAME', 'ADDRESS', 'TELEPHONE', 'EMAIL', 'MAIL', 'NUMBER'],
customer.values(),
customer1.values(),
customer2.values(),
]
print_pretty_table(table_data)
Результат:
NAME | ADDRESS | TELEPHONE | EMAIL | MAIL | NUMBER
-----+--------------------+--------------+--------------+---------------------+-------
Josh | Long st, Dallas TX | 555-987-1549 | [email protected] | On the mailing list | 1153
Djon | Long st, Dallas TX | 555-987-1549 | [email protected] | On the mailing list | 1153
Ser | Long st, Dallas TX | 555-987-1549 | [email protected] | On the
Объяснение:
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Ответ:
def print_pretty_table(data, cell_sep=' | ', header_separator=True):
rows = len(data)
cols = len(data[0])
col_width = []
for col in range(cols):
columns = [data[row][col] for row in range(rows)]
col_width.append(len(max(columns, key=len)))
separator = "-+-".join('-' * n for n in col_width)
for i, row in enumerate(range(rows)):
if i == 1 and header_separator:
print(separator)
result = []
for col in range(cols):
item = data[row][col].rjust(col_width[col])
result.append(item)
print(cell_sep.join(result))
Добавляем в Customer метод:
def values(self):
return self.get_name(), self.get_address(), self.get_telephone(), \
self.get_email(), self.mailList(),self.get_number()
И итог:
def main():
customer = Customer('Josh', 'Long st, Dallas TX', '555-987-1549','[email protected]','Y','1153')
customer1 = Customer('Djon', 'Long st, Dallas TX', '555-987-1549','[email protected]','Y','1153')
customer2 = Customer('Ser', 'Long st, Dallas TX', '555-987-1549','[email protected]','Y','1153')
table_data = [
['NAME', 'ADDRESS', 'TELEPHONE', 'EMAIL', 'MAIL', 'NUMBER'],
customer.values(),
customer1.values(),
customer2.values(),
]
print_pretty_table(table_data)
Результат:
NAME | ADDRESS | TELEPHONE | EMAIL | MAIL | NUMBER
-----+--------------------+--------------+--------------+---------------------+-------
Josh | Long st, Dallas TX | 555-987-1549 | [email protected] | On the mailing list | 1153
Djon | Long st, Dallas TX | 555-987-1549 | [email protected] | On the mailing list | 1153
Ser | Long st, Dallas TX | 555-987-1549 | [email protected] | On the
Объяснение: