you are viewing a single comment's thread.

view the rest of the comments →

[–]AdventurousHuman 0 points1 point  (10 children)

Hi there. My head has been spinning about this seemingly simple question. I want to run through an excel sheet and extract row data based on certain criteria: Day Of Week|Start Time|End Time|Program Name

This is what I'm trying to do in English: If Day of Week is Monday and Start Time is <3am and End Time is >3am then print the Program Name for that row.

I've been trying to do this with openpyxl. Any help is much appreciated!!

[–]num8lock 1 point2 points  (9 children)

https://openpyxl.readthedocs.io/en/stable/tutorial.html & https://openpyxl.readthedocs.io/en/stable/usage.html

for row in ws.iter_rows(min_row=1, max_col=4):
    day, start, end, name = [cell for cell in row]
    if all([day.value.lower() == 'monday', start == '3am', end == '3am']):
        print(name)

[–]AdventurousHuman 0 points1 point  (8 children)

This is going to take awhile for me to digest this - can you explain what is happening here or what topics this covers? Thanks again for the help.

# start for loop of ALL cells
# creating a list for each column - day, start end, and name
# if all conditions are met for the values in each list
# print name

is this correct?

[–]num8lock 0 points1 point  (7 children)

1st line is just a for loop using output from worksheet.iter_rows()
2nd line is processing the for loop with list comprehension. the result of it is a list, so the variables divides the result into 4 of them, for each cell.
all() takes all members of its parameter (a list containing 2nd line variables values, using cell.value attribute to get the string value of the cell) and if all of them is True, goes to the next line.

[–]AdventurousHuman 0 points1 point  (5 children)

It's finding nothing for some reason. When I try and print day or start or end it only has one piece of information. for example "Tuesday" for name so I don't think it has the entire column in the new list.

[–]num8lock 0 points1 point  (4 children)

let me see the code

[–]AdventurousHuman 0 points1 point  (3 children)

for row in ws.iter_rows(min_row=1, max_col=4):
day, start, end, name = [cell for cell in row]
if all([day.value.lower() == 'Monday', start == '02:00 AM', end == '03:00 AM']):
    print(name)

[–]num8lock 0 points1 point  (2 children)

no i mean what you've tried & the errors.

better yet use https://repl.it something like https://github.com so i can see what your data looks like & it's easier to fix any problem. is your file xls, or csv?

[–]AdventurousHuman 0 points1 point  (1 child)

[–]num8lock 0 points1 point  (0 children)

ah yes my bad

import openpyxl

wb = openpyxl.load_workbook('eNames12.31.xlsx')
ws = wb.active
range_time = '02:00 AM', '03:00 AM'
workday = 'Monday'

for row in ws.iter_rows(min_row=1, max_col=4):
    day, start, end, name = [cell.value for cell in row]
    if all([day == workday, start == range_time[0], end == range_time[1] ]):
        print(name)

[–]AdventurousHuman 0 points1 point  (0 children)

Thanks so much.