Created
November 30, 2015 15:06
-
-
Save jehiah/6b430466634b0f2a06ae to your computer and use it in GitHub Desktop.
Pivot a CSV table.
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
#!/usr/bin/env python | |
import sys | |
import csv | |
import tornado.options | |
from collections import defaultdict | |
def run(): | |
data = defaultdict(dict) | |
columns = set() | |
o = tornado.options.options | |
for line in csv.reader(sys.stdin): | |
columns.add(line[o.pivot_column]) | |
data[line[o.row_column]][line[o.pivot_column]] = line[o.value_column] | |
columns = sorted(columns) | |
w = csv.writer(sys.stdout) | |
w.writerow(['key'] + columns) | |
for key, row_data in data.items(): | |
row = [key] | |
for column in columns: | |
row.append(row_data.get(column, '')) | |
w.writerow(row) | |
if __name__ == "__main__": | |
tornado.options.define("pivot_column", default=1, type=int, help="column to pivot on (starting at 0)") | |
tornado.options.define("row_column", default=0, type=int, help="key for rows (starting at 0)") | |
tornado.options.define("value_column", default=2, type=int, help="value for rows (starting at 0)") | |
tornado.options.parse_command_line() | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment