Python Forum
Can a dictionary key be an integer?
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can a dictionary key be an integer?
#3
Thanks. I've been trying various approaches with no luck. Here is what I want to do. I thought a dictionary would be the best approach. I normally do this by hand, but I am reading the book "Automate the Boring Stuff" and this is boring! Students go away for a month to a dancing competition in Beijing, or a work experience in Shanghai. I move them to the bottom of my class lists. Slowly the order and the database order do not resemble each other. It's easy to get the wrong score next to a name.

#! /usr/bin/python3
# get the student number and score, then copy score to another file
# at the same time check to see if the student number corresponds to the score
# because the order of the students in the class register is not always the same as the database

import os, openpyxl

pathToExcel = input()
nameOfExcelFile = input()
saveFileName = nameOfExcelFile.split('.')

# this file exists
# it is the results file from the marking program saved as .xlsx
# looks like 16Eng4W4res.xlsx

wb1 = openpyxl.load_workbook(pathToExcel + nameOfExcelFile)

# this file only has 1 sheet so

activeSheetwb1 = wb1.active

# get max Row

maxRow = activeSheetwb1.max_row

# column 1 contains the score, column 2 the student numbers
# read the student numbers into a dictionary
# row 1 is just column headers, don't need these. Start at row 2
# StudentNumAndScore[activeSheetwb1.cell(row=rowNum, column=2).value] is the key
# the key is the student number
# activeSheetwb1.cell(row=rowNum, column=1).value is the score


StudentNumAndScore = {}

for rowNum in range(2, maxRow +1):
	StudentNumAndScore[activeSheetwb1.cell(row=rowNum, column=2).value] = activeSheetwb1.cell(row=rowNum, column=1).value

# now open the file to be written to
# the output file exists, just the student numbers are not in the same order
# as the marking program output
# I want to put each score against the correct student number
# I thought a dictionary would do this elegantly

outputFile = input()

wb2 = openpyxl.load_workbook(pathToExcel + outputFile)

# later on I can loop through sheetNameswb2

sheetNameswb2 = wb2.get_sheet_names()
activeSheetwb2 = wb2.get_sheet_by_name(sheetNameswb2[0])

# get the keys and put them in a list. How to do this directly from the dictionary??

keys = list(StudentNumAndScore.keys())

# for each key loop the rows in column 1 and find the key
# all these keys are in the active sheet, but I do not get any output
# nothing is written to "activeSheetwb2.cell(row=rowNum, column=4, value=StudentNumAndScore[key])"

for key in keys:
	for rowNum in range(2, maxRow + 1): # check each row for the key. Each key is present somewhere
		if key == activeSheetwb2.cell(row=rowNum, column=1).value:
			activeSheetwb2.cell(row=rowNum, column=4, value=StudentNumAndScore[key])

# save the file

wb2.save(pathToExcel + 'try1.xlsx')
I am not getting anything in try1.xlsx column 4!

Also, I'd like a more elegant use of the dictionary. I feel there is a better way to do this.

I can do this by directly comparing wb1 and wb2 in 2 loops.

Very grateful for any tips, suggestions, hints!
Reply


Messages In This Thread
Can a dictionary key be an integer? - by Pedroski55 - Sep-27-2017, 05:20 AM
RE: Can a dictionary key be an integer? - by buran - Sep-27-2017, 05:53 AM
RE: Can a dictionary key be an integer? - by Pedroski55 - Sep-28-2017, 01:02 PM
RE: Can a dictionary key be an integer? - by buran - Sep-28-2017, 01:29 PM
RE: Can a dictionary key be an integer? - by buran - Sep-30-2017, 02:23 PM
RE: Can a dictionary key be an integer? - by buran - Oct-05-2017, 05:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing an integer by looking at a dictionary wendysling 3 2,384 May-02-2019, 03:38 PM
Last Post: wendysling

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020