Created
August 5, 2011 19:46
-
-
Save arantius/1128338 to your computer and use it in GitHub Desktop.
Format Mozilla's "Application" stats into usable columns.
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
#!/usr/bin/env/python | |
"""Format Mozilla's "Application" stats into usable columns.""" | |
import csv | |
import re | |
import sys | |
reader = csv.reader(open(sys.argv[1])) | |
writer = csv.writer(sys.stdout) | |
keys_in = reader.next()[2:] | |
key_map = [] | |
for key in keys_in: | |
key_orig = key | |
key = re.sub(r'({.*}|unknown)/?', '', key) | |
if re.match(r'\d\.\d(\.|a|b|pre|$)', key): | |
key = re.sub(r'^(\d\.\d).*', r'\1', key) | |
else: | |
key = 'junk' | |
key_map.append(key) | |
writer.writerow(['date'] + sorted(list(set(key_map)))) | |
for record in reader: | |
tots = {} | |
for i, value in enumerate(record[2:]): | |
key = key_map[i] | |
tots.setdefault(key, 0) | |
tots[key] += int(value) | |
writer.writerow([record[0]] + [v for (k, v) in sorted(tots.items())]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment