Skip to content

Instantly share code, notes, and snippets.

@chaconnewu
Created June 22, 2018 16:22
Show Gist options
  • Save chaconnewu/014a34eb22de6de39dd637583302b212 to your computer and use it in GitHub Desktop.
Save chaconnewu/014a34eb22de6de39dd637583302b212 to your computer and use it in GitHub Desktop.
Excel XLSX DictReader in Python 3
import xlrd, mmap
def XLSDictReader(filename, sheet_index=0):
'''
Excel xlsd dict reader.
'''
with open(filename) as f:
book = xlrd.open_workbook(
file_contents=mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ))
sheet = book.sheet_by_index(sheet_index)
headers = dict((i, sheet.cell_value(0, i)) for i in range(sheet.ncols))
return (dict((headers[j], sheet.cell_value(i, j)) for j in headers)
for i in range(1, sheet.nrows))
r = XLSDictReader('somefile')
for line in r:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment