Skip to content

Instantly share code, notes, and snippets.

@khllkcm
Created October 17, 2016 16:29
Show Gist options
  • Save khllkcm/224861fafd29a39062548f458b0bef0e to your computer and use it in GitHub Desktop.
Save khllkcm/224861fafd29a39062548f458b0bef0e to your computer and use it in GitHub Desktop.
Python 3.5 script to calculate the maximum column length of a TSV file.
# -*- coding: utf-8 -*-
file_lines = open('C:\data_p2.txt', 'r').readlines() #list of file lines
fields_lengths = [[] for i in range(6)] #empty 2D list with 6 empty sublists (one for each column)
for line in file_lines:
fields = line.strip().split("\t") #list of the line's fields, strip() removes the tailing "\n".
for i in range(6):
fields_lengths[i].append(len(fields[i])) #each sublist stores the length of each field.
print([max(x) for x in fields_lengths]) #returns the maximum of each sublist; i.e. the maximum length of each column.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment