Creating Python Dictionaries from Excel Data: A Practical Guide with xlrd

Dec 06, 2025 · Programming · 9 views · 7.8

Keywords: Python | xlrd | Excel data processing

Abstract: This article provides a detailed guide on how to extract data from Excel files and create dictionaries in Python using the xlrd library. Based on best-practice code, it breaks down core concepts step by step, demonstrating how to read Excel cell values and organize them into key-value pairs. It also compares alternative methods, such as using the pandas library, and discusses common data transformation scenarios. The content covers basic xlrd operations, loop structures, dictionary construction, and error handling, aiming to offer comprehensive technical guidance for developers.

In data processing and analysis, extracting information from Excel files and converting it into Python dictionaries is a common requirement. This article explores how to achieve this using the xlrd library, based on a specific problem. Suppose we have an Excel file foo.xls and need to read data from the third worksheet (index 2), using values from column 2 as keys and column 0 as values to build a dictionary.

Core Implementation Method

The best-practice code uses the xlrd library to directly read the Excel file and construct a dictionary by iterating through rows. Here is a detailed breakdown of the code:

import xlrd

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

First, import the xlrd library and initialize an empty dictionary d. Use the xlrd.open_workbook function to open the Excel file and select the third worksheet via sheet_by_index(2). Loop through each row from 0 to 137, using sh.cell(i, 2).value to get the value from column 2 as the key and sh.cell(i, 0).value to get the value from column 0 as the value, then add it to the dictionary. This method is simple and efficient, suitable for scenarios with small data volumes and fixed structures.

Code Analysis and Optimization

During implementation, several key points should be noted. The loop range range(138) assumes the worksheet has 138 rows, which may cause errors if the row count differs. It is recommended to use sh.nrows to dynamically get the number of rows, e.g., for i in range(sh.nrows):. Additionally, ensure that cell value types are suitable as dictionary keys; if values are floats or special characters, conversion to strings may be necessary, such as str(cell_value_class). Error handling is also important, for example, using try-except blocks to catch cases like file not found or format errors.

Comparison with Other Methods

Beyond xlrd, the pandas library offers more advanced data processing capabilities. Referring to other answers, one can use pandas' ExcelFile and parse methods to read Excel files and convert them to dictionaries via to_dict(). For example:

from pandas import ExcelFile

xls = ExcelFile('path_to_file.xls')
df = xls.parse(xls.sheet_names[0])
print(df.to_dict())

This method is suitable for complex data transformations but may increase dependencies and memory usage. Another answer demonstrates converting Excel data into a list of dictionaries, applicable for scenarios where each row represents a record, but it differs from the dictionary structure in this problem.

Application Scenarios and Extensions

Creating dictionaries from Excel is useful for data mapping, configuration reading, and quick queries. For instance, in machine learning, dictionaries can store category labels; in web development, they can load configuration parameters. For extensions, consider handling multiple worksheets, adding data validation, or integrating into larger data processing workflows. By understanding core concepts, developers can adapt flexibly to various needs.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.