Sep-28-2017, 01:02 PM
(This post was last modified: Sep-28-2017, 01:02 PM by Pedroski55.)
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.
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!
#! /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!