Code error (Python, Pandas and Excel)

Asked

Viewed 113 times

0

I made a code to pick the time and day of the week and so picks up the cell of the excel spreadsheet and should inform me, but it is going wrong when picking up, besides on Tuesday (incicaçao ES) gives error.

An example of error is: today is Wednesday 12:53, it was supposed to be lunch but he says the value is "study history".

Column and row variable is giving the right value.

inserir a descrição da imagem aqui

import pandas as pd #importa para mecher no excel
from datetime import date #importa semna
from datetime import datetime #importa data

for nu in range(10000000000): #repete
    print("---------------------------------------------------------------------------------------") #separa só estetico
    hj = date.today() #pega o dia de hoje
    dias = ('Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado', 'Domingo') #lista dos dias
    print ("Hoje é", dias[hj.weekday()]) #fala que dia da semana é
    x = pd.read_excel(r"C:\Users\Victor\Desktop\rotina p.xlsx") #le a planilha
    semana = dias[hj.weekday()]  #Salva que dia da semana é

    minuto = datetime.today().minute #pega o minuto
    hora = datetime.today().hour #pega a hora

    #SE o dia for tal ele poe o nome da prmeira linha para pega a coluna
    if(semana == 'Segunda-feira'): 
        su = "AS"
    elif(semana == 'Terça-feira'):
       su = "ES"
    elif(semana == 'Quarta-feira'):
       su = "IS"
    elif(semana == 'Quinta-feira'):
        su = "OS"
    elif(semana == 'Sexta-feira'):
        su = "US"       
    elif(semana == 'Sábado'):
        su = "AO"
    elif(semana == 'Domingo'): 
        su = "AOS"

    #Ve se é antes ou depois de 30 minutos
    if(minuto >= 0 and minuto <= 30):
        print("antes dos 30")
        ates = True
    else:
        print("depois dos 30")
        ates = False

    #Fala que horas são
    if(hora == 7):
        print("agora são 7 horas")
    elif(hora == 8):
        print("agora são 8 horas")
    elif(hora == 9):
        print("agora são 9 horas")
    elif(hora == 10):
        print("agora são 10 horas")
    elif(hora == 11):
        print("agora são 11 horas")
    elif(hora == 12):
        print("agora são 12 horas")
    elif(hora == 13):
        print("agora são 13 horas")
    elif(hora == 14):
        print("agora são 14 horas")
    elif(hora == 15):
        print("agora são 15 horas")
    elif(hora == 16):
        print("agora são 16 horas")
    elif(hora == 17):
        print("agora são 17 horas")
    elif(hora == 18):
        print("agora são 18 horas")
    elif(hora == 19):
        print("agora são 19 horas")
    elif(hora == 20):
        print("agora são 20 horas")
    elif(hora == 21):
        print("agora são 21 horas")
    elif(hora == 22):
        print("agora são 22 horas")
    elif(hora == 23):
        print("agora são 23 horas")
    elif(hora == 24):
        print("agora são 24 horas")

    #Pega a hora
    if(hora == 7 and ates == False):
        cel = 3
    elif(hora == 8 and ates == True):
        cel=4
    elif(hora == 8 and ates == False):
        cel=5
    elif(hora == 9 and ates == True):
        cel=6
    elif(hora == 9 and ates == False):
        cel=7
    elif(hora == 10 and ates == True):
        cel=8
    elif(hora == 10 and ates == False):
        cel=9
    elif(hora == 11 and ates == True):
        cel=10
    elif(hora == 11 and ates == False):
        cel=11
    elif(hora == 12 and ates == True):
        cel=12
    elif(hora == 12 and ates == False):
        cel=13
    elif(hora == 13 and ates == True):
        cel=14
    elif(hora == 13 and ates == False):
        cel=15
    elif(hora == 14 and ates == True):
        cel=16
    elif(hora == 14 and ates == False):
        cel=17
    elif(hora == 15 and ates == True):
        cel = 18
    elif(hora == 15 and ates == False):
        cel = 19
    elif(hora == 16 and ates == True):
        cel = 20
    elif(hora == 16 and ates == False):
        cel = 21
    elif(hora == 17 and ates == True):
        cel = 22
    elif(hora == 17 and ates == False):
        cel = 23
    elif(hora == 18 and ates == True):
        cel = 24
    elif(hora == 18 and ates == False):
        cel = 25
    elif(hora == 19 and ates == True):
        cel = 26
    elif(hora == 19 and ates == False):
        cel = 27
    elif(hora == 20 and ates == True):
        cel = 28
    elif(hora == 20 and ates == False):
        cel = 29
    elif(hora == 21 and ates == True):
        cel = 30
    elif(hora == 21 and ates == False):
        cel = 31
    elif(hora == 22 and ates == True):
        cel = 32
    elif(hora == 22 and ates == False):
        cel = 33
    elif(hora == 23 and ates == True):
        cel = 34
    elif(hora == 23 and ates == False):
        cel = 35
    elif(hora == 24 and ates == True):
        cel = 36

    print(cel,su) #escreve a celula e coluna
    print(x[su][cel]) #printa a celula
  • I don’t play much with Pandas so I can’t comment on the error, but on the rest of the code, it’s simple quite a lot thing, mainly eliminate this lot of if/elif, see: https://repl.it/repls/PleasedBlandGames

  • Thanks for the simplification, I’m kind of new with python so only knew this method

  • I tried to redo the part of the panda he ta catching the cell 2 above (the same error) even the cell being right to temporarily solve I put cel = cel - 2 and then he takes the right value

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.