Python: Slow code execution and other problems

Asked

Viewed 235 times

0

Dear colleagues,

Can someone enlighten me on what might be going on?

In summary, it is a code to read cells from a spreadsheet, sort and transport to a new spreadsheet.

The problem: The code does not execute and shows no error. I am using Pycharm. I spent the day yesterday and today testing and, the interesting thing is that the DEBUG does not present error and runs the code quietly.

To understand what I’m creating: I took the results of LOTOFACIL (lottery game) and put in an excel spreadsheet. The spreadsheet was named lotofacil.xlsx. The spreadsheet has 1664 rows and 15 columns.

See an example of the spreadsheet...Planilha Excel 2007

Well, my code is like this...

    import openpyxl




wb = openpyxl.load_workbook('lotofacil.xlsx') #abre o arquivo
nome_plan = wb.sheetnames[0] #identifica o nome da planilha de indice zero
planilha = wb[nome_plan] #entra na planilha indicada


linhas = planilha.max_row #qtd de linhas
colunas = planilha.max_column #qtd de colunas

lista = [] #valores de cada linha são adicionados e depois excluídos


novo_arquivo = openpyxl.Workbook() #abre novo arquivo excel
nome_planNova = novo_arquivo.sheetnames[0] #identifica o nome da planilha de indice zero
planilhaNova = novo_arquivo[nome_planNova] #entra na planilha indicada
novo_arquivo.save('loto_python.xlsx') #salva a planilha nova


#cria um loop.
x=1
while linhas >= x: #enquanto o número de linhas da planilha (1664), for maior ou igual a 'x' o programa executa.
    y1=1
    while y1 <= colunas: #enquanto o número de colunas da planilha(15), for maior ou igual a 'y' o programa executa.
        celula = planilha.cell(row=x, column=y1).value #recebe o conteudo da celula.
        lista.append(celula) #adiciona o conteúdo da célula na lista. A lista terá o tamanho de 15 posições.
        y1+=1

    lista.sort() #ordena a lista, do menor para o maior.

    y2=1
    while y2 <= colunas:
        planilhaNova.cell(row=x, column=y2).value = lista[y2-1] #pega cada item da lista
        novo_arquivo.save('loto_python.xlsx') #salva novamente a planilha nova
        y2+=1

    lista = [] #esvazia a lista. Isso permite manter o loop e seguir para a linha seguinte.

    x+=1

See the output, already in the new spreadsheet... by DEBUG. Remembering: normal execution does not work. inserir a descrição da imagem aqui

  • The "normal execution does not work", you say it does not perform and does not give error. If it does not necessarily execute it will give error; or it executes, it just does not generate the expected result. Can you clarify better? You need to sort each row increasingly?

  • Of course Anderson, I clarify. I said, "The code does not execute and does not present any errors." Normal execution does not generate any kind of error alert and also does not generate the expected result. What is the expected result? Answer: The output shown in the attached image. The interesting thing is that DEBUG runs the program without error and generates the expected result, however, it does not help me. I would like the program to be automatic and not need me to have to run by DEBUG and keep pressing the "F8" key more than 53,248 times.

  • Can provide an example of input xlsx file that reproduces the error?

  • Hi Anderson. The input excel file is not the problem, once this only read the values. So far so good. The issue is that when I run the code, Pycharm does not display any error message, but creates the output excel file. Output excel does not open as it gives error.

  • The idea of making the file available is that we can run the code and try to reproduce the error. I believe you can even do a [mcve] on https://repl.it

  • Oh yes. Sure. I’ll do it now...

  • Ready. Follow the link to Google Drive. --> https://drive.google.com/open?id=1YKf-8OtIej_Oqpz2lrHBt5oQgOlHbplD

  • Hi Anderson. Wow! As soon as I finished talking to you, I put the code to run and I was waiting, waiting, waiting and, more than 1 (one) hour after the code finished the execution, no errors. The program worked! But I found it extremely time consuming. Is there any solution for this?!

  • Yeah, try putting the novo_arquivo.save only at the end of the file, out of the loop.

  • I did what you said... Executed in seconds. Thank you very much! By the way, let’s get rich, Anderson! hahahahaha A big hug!

Show 5 more comments

1 answer

1


Just to iterate the given solution, the way you did, calling the method save() inside the repeat loop, you will be saving the disk file multiple times (15 x 1664 = 24.960 times precisely). Read and write on disk is one of the slowest tasks on a computer, imagine doing this almost 25 thousand times with a file of 15 columns and 1664 rows. Obviously you don’t need to do this. Your object novo_arquivo already represents your entire file, so just modify it, persisting all changes only in memory and only at the end of the run persist them on disk only once.

To do this, simply place the code line:

novo_arquivo.save('loto_python.xlsx')

At the end of the program, out of all loops.

Browser other questions tagged

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