Created
July 23, 2012 13:37
-
-
Save amakelov/3163632 to your computer and use it in GitHub Desktop.
excel parsing
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
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