Skip to content

Instantly share code, notes, and snippets.

@amakelov
Created July 23, 2012 13:37
Show Gist options
  • Save amakelov/3163632 to your computer and use it in GitHub Desktop.
Save amakelov/3163632 to your computer and use it in GitHub Desktop.
excel parsing
import xlrd
from collections import namedtuple
def parse(filename):
book = xlrd.open_workbook(filename)
sheet = book.sheet_by_index(0)
cols = sheet.ncols
rows = sheet.nrows
init_row = 2
result = {}
rep = namedtuple('rep', ['name', 'party'])
for row in range(init_row, rows):
name = sheet.cell_value(rowx=row, colx=0).strip().upper()
party = sheet.cell_value(rowx=row, colx=3).strip().upper()
key = rep(name=name, party=party)
value = []
for col in range(4, cols):
value.append(sheet.cell_value(rowx=row, colx=col))
result[key] = tuple(value)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment