Created
June 22, 2018 16:22
-
-
Save chaconnewu/014a34eb22de6de39dd637583302b212 to your computer and use it in GitHub Desktop.
Excel XLSX DictReader in Python 3
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, 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