Skip to content

Instantly share code, notes, and snippets.

@nilsso
Created July 15, 2022 22:34
Show Gist options
  • Select an option

  • Save nilsso/23c64d47100fcac87a226baa385f4d18 to your computer and use it in GitHub Desktop.

Select an option

Save nilsso/23c64d47100fcac87a226baa385f4d18 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import functools
import argparse
import json
from pathlib import Path
import typing
import csv
def optimal_widths(in_file: typing.IO, delimiter: str):
reader = csv.reader(in_file, delimiter=delimiter)
headers = next(reader)
widths = functools.reduce(
lambda w, r: list(map(max, zip(w, map(len, r)))),
reader,
[0] * len(headers),
)
return dict(zip(headers, widths))
DESCRIPTION = """\
Determine max column widths for CSV.
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser("clean_roll", description=DESCRIPTION)
parser.add_argument("in_path", type=Path)
parser.add_argument("-d", "--delim", default="|")
args = parser.parse_args()
with open(args.in_path, "r") as f:
res = optimal_widths(f, args.delim)
print(json.dumps(res, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment