Kim Minseo
Kim Minseo

Reputation: 81

KeyError when accessing dictionary made from Excel file

I am attempting to store data in an excel file as a dictionary and then compare its keys against user-inputted string.

datesData = pd.read_excel('dates.xlsx', index_col = 0, header = 0)
datesDict = datesData.to_dict()
applyDate = input("Enter the date (YYYY-MM-DD): ")
value = datesDict[applyDate]

However, this raises a KeyError and is unable to retrieve the data. The Excel file looks like this: Excel File Example

Date is entered as Text and I have checked that datesDict keys are in the form of String data type.

What am I doing wrong here?

Upvotes: 0

Views: 187

Answers (1)

Zoro
Zoro

Reputation: 423

The dictionary datesDict looks like this:

>>> datasDict
{'LeavesApplied': {'2021-01-01': -1, '2021-01-02':0,
 '2021-01-03': 0, '2021-01-04': 0, '2021-01-05': 0,
 '2021-01-06': 0, '2021-01-07': 0, '2021-01-08': 0,
 '2021-01-09': 0, '2021-01-10': 0, '2021-01-11': 0,
 '2021-01-12': 0, '2021-01-13': 0, '2021-01-14': 0,
 '2021-01-15': 0, '2021-01-16': 0, '2021-01-17': 0,
 '2021-01-18': 0, '2021-01-19': 0, '2021-01-20': 0,
 '2021-01-21': 0, '2021-01-22': 0}
}

You can extract values for corresponding Date, like:

value = datesDict['LeavesApplied'][applyDate]

Upvotes: 2

Related Questions