Last active
August 29, 2015 14:19
-
-
Save wehappyfew/e4eecca3e4235537376e to your computer and use it in GitHub Desktop.
Parse an excel file and grab the values in the columns and save them in dictionaries
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
def fetch_excel_values(filepath, sheet_index=0): | |
from xlrd import open_workbook | |
# open the excel file | |
book = open_workbook(filename=filepath) | |
# open the specific sheet based on index | |
sheet = book.sheet_by_index(0) | |
# read headers | |
keys = [sheet.cell(int(sheet_index),col_index).value for col_index in range(sheet.ncols)] | |
# fetch all the values from the excel and create a list | |
dict_list = [] | |
for row_index in xrange(1,sheet.nrows): | |
d = { | |
keys[col_index]:sheet.cell(row_index,col_index).value | |
for col_index in range(sheet.ncols) | |
} | |
dict_list.append(d) | |
# initialize the dictionaries | |
email_pass = {} #; em_name = {} | |
username_pass = {} #; up_name = {} | |
# iterate through the excel values and populate the dictionaries | |
for item in dict_list: | |
if item["username"] == " ": | |
email_pass.update( {item["email"]: int(item["password"])} ) | |
#em_name.update( {item["firstname"] : item["lastname"]} ) | |
elif item["email"] == " ": | |
username_pass.update( {item["username"]: int(item["password"])} ) | |
#up_name.update( {item["firstname"] : item["lastname"]} ) | |
return email_pass, | |
#em_name , | |
username_pass, | |
#up_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment