Skip to content

Instantly share code, notes, and snippets.

@philikon
Created March 1, 2010 16:43
Show Gist options
  • Save philikon/318535 to your computer and use it in GitHub Desktop.
Save philikon/318535 to your computer and use it in GitHub Desktop.
"""
Takes CSV locale statistics from addons.mozilla.org and sums numbers
for same languages (e.g. es = es + es_ES + es_MX + ...). Output is
similarly formatted CSV.
"""
import sys
languages = {}
for line in open(sys.argv[-1]):
if line.startswith('# Fields'):
line = line[11:-2]
locales = line.split(';')[2:]
for i, locale in enumerate(locales):
chunks = locale.split('-')
languages.setdefault(chunks[0], []).append(i)
print 'date,total,' + ','.join(sorted(languages))
continue
if line.startswith('#'):
continue
data = line.split(',');
date, total = data.pop(0), data.pop(0)
data = [int(value) for value in data]
print (
date + ',' + total + ',' +
','.join(str(sum(data[i] for i in positions))
for lang, positions in sorted(languages.items()))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment