def print_tbl(file: str, explicit_headers: Optional[List[str]], select: Optional[List[str]], format: str): try: with open(file) as file: reader = csv.reader(file) try: headers = explicit_headers or next(reader) except StopIteration: raise click.UsageError("csv file is empty") if select is not None: # Verify that at least some of the selected headers are present if len(set(headers).intersection(set(select))) == 0: raise click.UsageError("The selected headers dont match any of the headers found. ({})\n" "Hint: Headers are case sensitive" .format(headers)) selected_headers = select or headers # Create a list of dictionaries with the data table = [{i: v for i, (h, v) in enumerate(zip(headers, row)) if h in selected_headers} for row in reader] except FileNotFoundError: raise click.FileError(file, "Not found") rapidtables.print_table(table, tablefmt=format, wrap_text=True, headers=selected_headers, max_column_width=find_max_column_width(selected_headers), allow_multiline=True, align=rapidtables.ALIGN_LEFT)
async def go(): pool = await aiopg.create_pool(dsn) async with pool.acquire() as conn: async with conn.cursor() as cur: await cur.execute(*query_and_params) headers = [d.name for d in cur.description] data = [] async for row in cur: data.append(dict(zip(headers, row))) pool.close() await pool.wait_closed() print("ALL DONE") rapidtables.print_table(data)
'salary': 1800, 'job': 'Q/A' } ] # colorize every single column header, rows = format_table(data, fmt=2) spacer = ' ' print(colored(spacer.join(header), color='blue')) print(colored('-' * sum([(len(x) + 2) for x in header]), color='grey')) for r in rows: print(colored(r[0], color='white', attrs=['bold']) + spacer, end='') print(colored(r[1], color='cyan') + spacer, end='') print(colored(r[2], color='yellow')) print() # colorize only rows header, rows = format_table(data, fmt=1) print(colored(header, color='blue')) print(colored('-' * len(header), color='grey')) for r in rows: print(colored(r, color='yellow')) print() # print raw for fmt in ('raw', 'simple', 'md', 'rst'): print_table(data, tablefmt=fmt) print()
print(colored(spacer.join(header), color='blue')) print(colored('-' * sum([(len(x) + 2) for x in header]), color='grey')) for r in rows: cols = ((colored(r[0], color='white', attrs=['bold'])), (colored(r[1], color='cyan')), (colored(r[2], color='yellow'))) print(spacer.join(cols)) print('') # colorize only rows, custom header. may not work properly, use ordered dicts # for input data rows header, rows = format_table(data, headers=('first name', 'income', 'position'), fmt=FORMAT_GENERATOR) print(colored(header, color='blue')) print(colored('-' * len(header), color='grey')) for r in rows: print(colored(r, color='yellow')) print('') print_table(data, tablefmt='raw', align=(ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT)) print('') print_table(data, tablefmt='simple', align=ALIGN_LEFT) print('') print_table(data, tablefmt='md', align=ALIGN_CENTER) print('') print_table(data, tablefmt='rst', align=ALIGN_RIGHT) print('')
from rapidtables import format_table, print_table from termcolor import colored # colorize every single column header, rows = format_table(data, fmt=2) spacer = ' ' print(colored(spacer.join(header), color='blue')) print(colored('-' * sum([(len(x) + 2) for x in header]), color='grey')) for r in rows: cols = ((colored(r[0], color='white', attrs=['bold'])), (colored(r[1], color='cyan')), (colored(r[2], color='yellow'))) print(spacer.join(cols)) print('') # colorize only rows, custom header. may not work properly, use ordered dicts # for input data rows header, rows = format_table(data, headers=('first name', 'income', 'position'), fmt=1) print(colored(header, color='blue')) print(colored('-' * len(header), color='grey')) for r in rows: print(colored(r, color='yellow')) print('') # print raw, don't align numbers to right for rst for fmt in ('raw', 'simple', 'md', 'rst'): print_table(data, tablefmt=fmt, align=0 if fmt == 'rst' else 1) print('')
'job': 'Batman' }, { 'rating': 8, 'job': 'Lords of the rings return of the king extended theatrical edition' }, { 'rating': 7, 'job': 'Superman' }] from rapidtables import print_table print_table(data, allow_multiline=True, tablefmt='simple', max_column_width=10, wrap_text=True) print('') print_table(data, allow_multiline=True, tablefmt='md', max_column_width=10, wrap_text=True) print('') print_table(data, allow_multiline=True, tablefmt='rst', max_column_width=10, wrap_text=True) print('')
(colored(r[1], color='cyan')), (colored(r[2], color='yellow'))) print(spacer.join(cols)) print('') # colorize only rows, custom header. may not work properly, use ordered dicts # for input data rows header, rows = format_table(data, headers=('first name', 'income', 'position'), fmt=FORMAT_GENERATOR, multiline=MULTILINE_EXTENDED_INFO) print(colored(header, color='blue')) print(colored('-' * len(header), color='grey')) for r in rows: print(colored(r[1], color='white' if r[0] else 'yellow')) print('') print_table(data, tablefmt='raw', align=(ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT), allow_multiline=True) print('') print_table(data, tablefmt='simple', align=ALIGN_LEFT, allow_multiline=True) print('') print_table(data, tablefmt='md', align=ALIGN_CENTER, allow_multiline=True) print('') print_table(data, tablefmt='rst', align=ALIGN_RIGHT, allow_multiline=True) print('') print_table(data, tablefmt='rstgrid', allow_multiline=True) print('')