Python Creating Dictionary from excel data

Question:

I want to create a dictionary from the values, i get from excel cells,
My code is below,

wb = xlrd.open_workbook('foo.xls')
sh = wb.sheet_by_index(2)   
for i in range(138):
    cell_value_class = sh.cell(i,2).value
    cell_value_id = sh.cell(i,0).value

and I want to create a dictionary, like below, that consists of the values coming from the excel cells;

{'class1': 1, 'class2': 3, 'class3': 4, 'classN':N}

Any idea on how I can create this dictionary?

Asked By: tuna

||

Answers:

d = {}
wb = xlrd.open_workbook('foo.xls')
sh = wb.sheet_by_index(2)   
for i in range(138):
    cell_value_class = sh.cell(i,2).value
    cell_value_id = sh.cell(i,0).value
    d[cell_value_class] = cell_value_id
Answered By: eumiro

or you can try pandas

from pandas import *
xls = ExcelFile('path_to_file.xls')
df = xls.parse(xls.sheet_names[0])
print df.to_dict()
Answered By: root

I’d go for:

wb = xlrd.open_workbook('foo.xls')
sh = wb.sheet_by_index(2)   
lookup = dict(zip(sh.col_values(2, 0, 138), sh.col_values(0, 0, 138)))
Answered By: Jon Clements

if you can convert it to csv this is very suitable.

import dataconverters.commas as commas
filename = 'test.csv'
with open(filename) as f:
      records, metadata = commas.parse(f)
      for row in records:
            print 'this is row in dictionary:'+row
Answered By: hamed

This script allows you to transform an excel data table to a list of dictionaries:

import xlrd

workbook = xlrd.open_workbook('foo.xls')
workbook = xlrd.open_workbook('foo.xls', on_demand = True)
worksheet = workbook.sheet_by_index(0)
first_row = [] # The row where we stock the name of the column
for col in range(worksheet.ncols):
    first_row.append( worksheet.cell_value(0,col) )
# transform the workbook to a list of dictionaries
data =[]
for row in range(1, worksheet.nrows):
    elm = {}
    for col in range(worksheet.ncols):
        elm[first_row[col]]=worksheet.cell_value(row,col)
    data.append(elm)
print data
Answered By: khelili miliana

You can use Pandas to do this. Import pandas and Read the excel as a pandas dataframe.

import pandas as pd
file_path = 'path_for_your_input_excel_sheet'
df = pd.read_excel(file_path, encoding='utf-16')

You can use pandas.DataFrame.to_dict to convert a pandas dataframe to a dictionary. Find the documentation for the same here

df.to_dict()

This would give you a dictionary of the excel sheet you read.

Generic Example :

df = pd.DataFrame({'col1': [1, 2],'col2': [0.5, 0.75]},index=['a', 'b'])

>>> df

col1 col2
a 1 0.50
b 2 0.75

>>> df.to_dict()

{'col1': {'a': 1, 'b': 2}, 'col2': {'a': 0.5, 'b': 0.75}}

Answered By: Amog Chandrashekar

There is also a PyPI package for that: https://pypi.org/project/sheet2dict/
It is parsing excel and csv files and returning it as an array of dictionaries.
Each row is represented as a dictionary in the array.

Like this way :

Python 3.9.0 (default, Dec  6 2020, 18:02:34)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

# Import the library
>>> from sheet2dict import Worksheet

# Create an object
>>> ws = Worksheet()

# return converted rows as dictionaries in the array 
>>> ws.xlsx_to_dict(path='Book1.xlsx')
[
    {'#': '1', 'question': 'Notifications Enabled', 'answer': 'True'}, 
    {'#': '2', 'question': 'Updated', 'answer': 'False'}
]
Answered By: Tomas Pytel

If you want to convert your Excel data into a list of dictionaries in python using pandas,
Best way to do that:

excel_file_path = 'Path to your Excel file'
excel_records = pd.read_excel(excel_file_path)
excel_records_df = excel_records.loc[:, ~excel_records.columns.str.contains('^Unnamed')]
records_list_of_dict=excel_records_df.to_dict(orient='record')
Print(records_list_of_dict)
Answered By: Mobin Al Hassan

If you use, openpyxl below code might help:

import openpyxl
workbook = openpyxl.load_workbook("ExcelDemo.xlsx")
sheet = workbook.active
first_row = [] # The row where we stock the name of the column
for col in range(1, sheet.max_column+1):
    first_row.append(sheet.cell(row=1, column=col).value)
data =[]
for row in range(2, sheet.max_row+1):
    elm = {}
    for col in range(1, sheet.max_column+1):
        elm[first_row[col-1]]=sheet.cell(row=row,column=col).value
    data.append(elm)
print (data)

credit to: Python Creating Dictionary from excel data

Answered By: Raam Kumar

I tried a lot of ways but this is the most effective way I found:

import pyexcel as p

def read_records():
    records = p.get_records(file_name="File")
    products = [row for row in records]
    return products
Answered By: Abdul Rehman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.