Last active
November 22, 2023 21:54
-
-
Save hdary85/bf954d874360757b29b0d5f48f437049 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Open the workbook | |
workbook = xlrd.open_workbook('your_excel_file.xls') | |
# Select the first sheet | |
sheet = workbook.sheet_by_index(0) | |
# Iterate through rows and columns to read data | |
for row in range(sheet.nrows): | |
for col in range(sheet.ncols): | |
cell_value = sheet.cell_value(row, col) | |
print(f'Row {row}, Col {col}: {cell_value}') | |
# Create an empty dictionary | |
data_dict = {} | |
# Iterate through rows and columns to populate the dictionary | |
for row_index in range(sheet.nrows): | |
row_data = {} | |
for col_index in range(sheet.ncols): | |
col_name = sheet.cell_value(0, col_index) # Assuming the first row contains column names | |
cell_value = sheet.cell_value(row_index, col_index) | |
row_data[col_name] = cell_value | |
data_dict[row_index] = row_data | |
# Print the resulting dictionary | |
print(data_dict) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment