Created
February 12, 2017 03:38
-
-
Save sr105/04063c756db154b5df383892c021a7fb to your computer and use it in GitHub Desktop.
Re-implement `column -t filename` using python and centering the columns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import itertools | |
def three_passes_and_storage(): | |
# Read file and split each line into fields (by whitespace) | |
with open(sys.argv[1]) as f: | |
lines = [line.split() for line in f.readlines()] | |
# Check that each line has the same number of fields | |
num_fields = len(lines[0]) | |
for n, line in enumerate(lines): | |
if len(line) != num_fields: | |
print('Line {} has wrong number of columns: expected {}, got {}'.format(n, num_fields, len(line))) | |
sys.exit(1) | |
# Calculate the maximum length of each field | |
max_column_widths = [0] * num_fields | |
for line in lines: | |
line_widths = (len(field) for field in line) | |
max_column_widths = [max(z) for z in zip(max_column_widths, line_widths)] | |
# Now print them centered using the max_column_widths | |
spacing = 4 | |
format_spec = (' ' * spacing).join('{:^' + str(n) + '}' for n in max_column_widths) | |
for line in lines: | |
print(format_spec.format(*line)) | |
def two_passes(): | |
# Read file and split each line into fields (by whitespace) | |
with open(sys.argv[1]) as f: | |
line = f.readline().split() | |
num_fields = len(line) | |
max_column_widths = (len(field) for field in line) | |
for n, line in enumerate(f, start=1): | |
line = line.split() | |
if len(line) != num_fields: | |
print('Line {} has wrong number of columns: expected {}, got {}'.format(n, num_fields, len(line))) | |
sys.exit(1) | |
line_widths = (len(field) for field in line) | |
max_column_widths = [max(z) for z in zip(max_column_widths, line_widths)] | |
# Now print them centered using the max_column_widths | |
spacing = 4 | |
format_spec = (' ' * spacing).join('{:^' + str(n) + '}' for n in max_column_widths) | |
with open(sys.argv[1]) as f: | |
for line in f: | |
print(format_spec.format(*line.split())) | |
def two_passes_and_storage(): | |
# Read file and split each line into fields (by whitespace) | |
with open(sys.argv[1]) as f: | |
line = f.readline().split() | |
num_fields = len(line) | |
max_column_widths = (len(field) for field in line) | |
lines = [line] | |
for n, line in enumerate(f, start=1): | |
line = line.split() | |
lines.append(line) | |
if len(line) != num_fields: | |
print('Line {} has wrong number of columns: expected {}, got {}'.format(n, num_fields, len(line))) | |
sys.exit(1) | |
line_widths = (len(field) for field in line) | |
max_column_widths = [max(z) for z in zip(max_column_widths, line_widths)] | |
# Now print them centered using the max_column_widths | |
spacing = 4 | |
format_spec = (' ' * spacing).join('{:^' + str(n) + '}' for n in max_column_widths) | |
for line in lines: | |
print(format_spec.format(*line)) | |
three_passes_and_storage() | |
print('=' * 40) | |
two_passes() | |
print('=' * 40) | |
two_passes_and_storage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment