Skip to content

Instantly share code, notes, and snippets.

@fzembow
Created August 4, 2020 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzembow/8e3cd149902497cb494ae5c80ae94b90 to your computer and use it in GitHub Desktop.
Save fzembow/8e3cd149902497cb494ae5c80ae94b90 to your computer and use it in GitHub Desktop.
Prints the largest field lengths encountered in a CSV
import csv
import sys
from collections import defaultdict
def print_max_field_lengths(fname):
"""
Prints the largest field lengths encountered in the csv
file in fname
"""
counts = defaultdict(int)
f = open(fname)
with f as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
for k, v in row.items():
counts[k] = max(counts[k], len(v))
for (k, count) in reversed(sorted(counts.items(), key=lambda x: x[1])):
print(count, k)
if __name__ == "__main__":
print_max_field_lengths(sys.argv[-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment