Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Read excel file and return all the rows as dict fields. ;)
import xlrd
def getDataFromFile(fileName):
with xlrd.open_workbook(fileName) as wb:
# we are using the first sheet here
worksheet = wb.sheet_by_index(0)
# getting number or rows and setting current row as 0 -e.g first
num_rows, curr_row = worksheet.nrows - 1, 0
# retrieving keys values(first row values)
keyValues = [x.value for x in worksheet.row(0)]
# building dict
data = dict((x, []) for x in keyValues)
# iterating through all rows and fulfilling our dictionary
while curr_row < num_rows:
curr_row += 1
for idx, val in enumerate(worksheet.row(curr_row)):
if val.value.strip():
data[keyValues[idx]].append(val.value)
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.