I would like to make the first column of each row in an excel spreadsheet as a key and rest of the values in that row as its value so that I can store them in a dictionary. The problem is, when I loop through the rows and columns all the row values are getting stored in every key.
import openpyxl
from openpyxl import load_workbook
file = "test.xlsx"
#load the work book
wb_obj = load_workbook(filename = file)
wsheet = wb_obj['test']
#dictionary to store data
dataDict = {}
value = []
row_count = wsheet.max_row
col_count = wsheet.max_column
# loop to get row and column values
for i in range(2, row_count+1):
for j in range(i, col_count+1):
key = wsheet.cell(row=i, column=1).value
print (key)
value.append(wsheet.cell(row=i, column=j).value)
print (value)
dataDict[key] = value
#prompt user for input
userInput = input("Please enter an id to find a person's details: ")
print (dataDict.get(int(userInput)))
Result I'm expecting: {1: ['John', 'Doe', 4567891234, '[email protected]'], 2: ['Wayne 'Kane', 1234567891, '[email protected]']}
Result I got: {1: ['John', 'Doe', 4567891234, '[email protected]', 'Kane', 1234567891, '[email protected]'], 2: ['John', 'Doe', 4567891234, '[email protected]', 'Kane', 1234567891, '[email protected]']}