Created
September 21, 2015 19:15
-
-
Save cthoyt/2107cb0871ecd043b43e to your computer and use it in GitHub Desktop.
Jennie's Unpivot
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
# 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