Skip to content

Instantly share code, notes, and snippets.

@cthoyt
Created September 21, 2015 19:15
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 cthoyt/2107cb0871ecd043b43e to your computer and use it in GitHub Desktop.
Save cthoyt/2107cb0871ecd043b43e to your computer and use it in GitHub Desktop.
Jennie's Unpivot
# open up the file
with open("filepath") as f:
# read the first line, this contains all of the header information
# then, get rid of whitespace with strip, and split into an array on each tab
header = f.readline().strip().split("\t")
# only keep the important header columns (delete leftmost element)
header = header[1:]
# iterate over each remaining line
for line in f:
# do the same sort of cleanup as before
line = line.strip().split("\t")
# get the key for this line (AKA far left column)
key = line[0]
# get rest of data for this line
data = line[1:]
# iterate over each column, and print out a new line with the
# original key, the name of the column, then the corresponding
# datum
for value, datum in zip(header, data):
print(key, value, datum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment