Skip to content

Instantly share code, notes, and snippets.

@frizz925
Created October 16, 2019 06:04
Show Gist options
  • Save frizz925/a02ee9645fc7b0f476c80ca34eeb9008 to your computer and use it in GitHub Desktop.
Save frizz925/a02ee9645fc7b0f476c80ca34eeb9008 to your computer and use it in GitHub Desktop.
Simple Python script that reads CSV from stdin then pretty-print them into a table
#!/usr/bin/env python3
import csv
import sys
from typing import Iterable, List
def main():
content_lines = sys.stdin.buffer.readlines()
reader = csv.reader(line.decode('utf-8') for line in content_lines)
headers = next(reader)
print(create_table(reader, headers))
def create_table(rows: Iterable[List[str]], headers: List[str]) -> str:
table = [headers]
column_lengths = [len(header) for header in headers]
for row in rows:
for i, text in enumerate(row):
column_length = column_lengths[i]
text_length = len(text)
if text_length > column_length:
column_lengths[i] = text_length
table.append(list(row))
result = []
for row in table:
row_text = []
for i, text in enumerate(row):
column_length = column_lengths[i]
row_text.append(space_pad(text, column_length))
result.append(' '.join(row_text))
return '\n'.join(result)
def space_pad(text: str, length: int) -> str:
temp = text + ''.join(' ' for _ in range(length))
return temp[:length]
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment