Nov-10-2019, 11:24 PM
here's one of my simple commands. when you have output from a command that has columns that are not aligned vertically, this script command lines it up by getting the maximum width of each column then justifying each column of each row to that size, padded with spaces, left justified, except for decimal numbers right justified. it also takes data in arguments as a convenience for shell scripts.
http://ipal.net/python/align_columns.py
http://ipal.net/python/align_columns.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Line up columns of input text.""" from sys import argv,stdin argv.pop(0) size = [] # indexed by col number rows = [] # indexed by row number for data in argv if argv else stdin: # in case an argument has 2 or more lines; for line in data.splitlines(): cols = tuple(line.split()) rows.append(cols) x = len(cols) y = len(size) if y<x: size[:] = size+(x-y)*[1] for n in range(x): size[n] = max(size[n],len(cols[n])) for row in rows: new = [] n = 0 for col in row: if col.isdecimal(): new.append(col.rjust(size[n])) else: new.append(col.ljust(size[n])) n += 1 print(' '.join(new).rstrip())
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.