Converting Excel File into a Dictionary using xlrd module
from xlrd import open_workbook
import json
import os
os.chdir("d:/2018/mayank")
def parse_xlsx():
workbook = open_workbook('demo.xlsx')
sheets = workbook.sheet_names()
active_sheet = workbook.sheet_by_name(sheets[0])
num_rows = active_sheet.nrows
num_cols = active_sheet.ncols
header = [active_sheet.cell_value(0, cell).lower() for cell in range(num_cols)]
for row_idx in range(1, num_rows):
row_cell = [active_sheet.cell_value(row_idx, col_idx) for col_idx in range(num_cols)]
yield dict(zip(header, row_cell))
for row in parse_xlsx():
print (row)
import json
import os
os.chdir("d:/2018/mayank")
def parse_xlsx():
workbook = open_workbook('demo.xlsx')
sheets = workbook.sheet_names()
active_sheet = workbook.sheet_by_name(sheets[0])
num_rows = active_sheet.nrows
num_cols = active_sheet.ncols
header = [active_sheet.cell_value(0, cell).lower() for cell in range(num_cols)]
for row_idx in range(1, num_rows):
row_cell = [active_sheet.cell_value(row_idx, col_idx) for col_idx in range(num_cols)]
yield dict(zip(header, row_cell))
for row in parse_xlsx():
print (row)
Comments
Post a Comment