Skip to content

Instantly share code, notes, and snippets.

@JoeGermuska
Created March 7, 2012 22:59
Show Gist options
  • Save JoeGermuska/1996940 to your computer and use it in GitHub Desktop.
Save JoeGermuska/1996940 to your computer and use it in GitHub Desktop.
Example of script to make a csvkit fixed width schema from IL Board of Ed School Report Card file layouts
#!/usr/bin/env python
import sys
import csv
input_file = sys.argv[1]
r = csv.reader(open(input_file))
w = csv.writer(sys.stdout)
w.writerow(['column','start','length'])
for row in r:
try:
if row[0].isdigit():
col = row[5].strip()
col_plus = filter(None,map(str.strip,row[1:3]))
if col_plus:
col = "%s (%s)" % (col,','.join(col_plus))
start = int(row[7])
length = int(row[8]) - start
w.writerow([col,start,length])
except Exception,e:
sys.stderr.write("%s\n" % e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment