Last active
December 26, 2015 03:55
-
-
Save mikeboers/4997162 to your computer and use it in GitHub Desktop.
Python script to transpose tab-delimited data (e.g. copy-pasted data from a spreadsheet).
This file contains 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
import csv | |
import itertools | |
import sys | |
# Read all Tab-delimited rows from stdin. | |
tsv_reader = csv.reader(sys.stdin, delimiter='\t') | |
all_data = list(tsv_reader) | |
# Transpose it. | |
all_data = list(itertools.izip_longest(*all_data, fillvalue='')) | |
# Write it back out. | |
tsv_writer = csv.writer(sys.stdout, delimiter='\t') | |
for row in all_data: | |
tsv_writer.writerow(row) |
This file contains 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
import sys | |
# Read all Tab-delimited rows from stdin. | |
all_data = [] | |
for line in sys.stdin: | |
all_data.append(line.strip().split('\t')) | |
# Transpose it. | |
transposed = [] | |
for row_i in range(len(all_data[0])): | |
new_row = [] | |
for col_i in range(len(all_data)): | |
new_row.append(all_data[col_i][row_i]) | |
transposed.append(new_row) | |
# Write it back out. | |
for row in transposed: | |
print '\t'.join(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment