Skip to content

Instantly share code, notes, and snippets.

@danielcodes
Created January 22, 2016 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielcodes/9840c91f8e94e5177aec to your computer and use it in GitHub Desktop.
Save danielcodes/9840c91f8e94e5177aec to your computer and use it in GitHub Desktop.
#table formatting example
# lists to create table
header = ['First name', 'Last name']
first = ['Daniel', 'Ronnie', 'Vanessa']
last = ['Chia', 'Hernandez', 'Luka']
# define variables
col_count = 2
col_widths = [10, 9]
formatters = ['{:10}', '{:9}']
print('\n')
# start building the list
result = []
# append header
print('====== this is a first append')
result.append(
formatters[i].format(header[i])
for i in range(col_count))
print(result)
print('\n')
# append line separator
print('====== this is a second append')
result.append(
('=' * col_widths[i]
for i in range(col_count)))
for i in result:
print(i)
print('\n')
# append data rows
print('====== this is a third append')
data = (first, last) #unpacked with *
for row in zip(*data):
result.append(
[formatters[i].format(row[i])
for i in range(col_count)])
for i in result:
print(i)
print('\n')
#join the each row with spaces
print('====== Join the collections inside with spaces')
result = (' '.join(r) for r in result)
print(result)
print('\n')
print('====== Final result, join collections with new lines')
print('\n'.join(result))
print('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment