Obtaining data from excel and turning it into a list by Python

Asked

Viewed 1,200 times

1

import xlrd 

def ler_arquivo(teste_termal1):
    tt1= xlrd.open_workbook('teste_termal1.xls', formatting_info=True)
    eajp60= workbook_r.sheet_by_index(3)
    Coluna_4=[3]
    Coluna_13=[12]
    return {
    "Ct":Coluna_4,
    "Sa":Coluna_13,
    }
    CT=[Ct]
    SA=[Sa]
for i in range(len(SA)):
    satxct=CT*SA
print CT, SA , satxct

I wrote with your help this small script, in it I try to fetch the values of a column in an excel page, so I found the logic good, but it says that the "Ct" is not defined, so how do I define this "Ct"?

  • On what line does the error occur? Your code is a little confused: I didn’t understand the part CT=[Ct] after function return. Question 1: This should not be outside the function, i.e., not indented along with the rest of the code (the for and the print)? Question 2: What you wished to do while using CT=[Ct]? After all, its function seems to return a Dictionary, then this value would need to be stored in a variable for the "Ct" field to be accessed via string indexing (for example, like http://stackoverflow.com/a/3765543/2896619)

  • 1

    Another confusing thing is this kind of instruction: Coluna_13=[12]. I don’t understand the intention, but what it does is create a list/vector with a single element 12: open the console and run: >>> Coluna_13=[12]; then perform: >>> Coluna_13; will return: [12]. Was your intention not to do, in that case: Coluna_13=eajp60[12]?

  • The Coluna_13 instruction is just to know that I am referring to column 13 of Excel, and to rescue it as "[12]" since it is an entire column. The "CT" in my view should be a list that contains the data of Coluna_4, and SA of the coluna_13. I’m wanting to returns the column values in a list.

  • It follows from where I tried to get this code Link

1 answer

3

There are several errors in this code. The corrected version would look like this:

import xlrd 

def ler_arquivo():
    tt1 = xlrd.open_workbook('teste_termal1.xls', formatting_info=True)
    eajp60 = workbook_r.sheet_by_index(3)
    dicionario = {}
    dicionario["Ct"] = eajp60[3]
    dicionario["Sa"] = eajp60[12]
    return dicionario

dicionario_retorno = ler_arquivo()
satxct = dicionario["Ct"] * dicionario["Sa"]

print(dicionario["Ct"], dicionario["Sa"], satxct)

I will discuss part of the amendments. In the function statement:

def ler_arquivo(teste_termal1):

You don’t use the variable teste_termal1, so I pulled her out.

Here:

Coluna_4=[3]
Coluna_13=[12]

Python variables usually do not follow this capitalization start convention. Since they are of no use, I also removed them and exchanged them for a dictionary, which I assume was the initial goal.

This here:

Coluna_4=[3]

Defines a variable Coluna_4 as a list with a single element. In this case, 3. In the same way, this:

Coluna_13=[12]

Also defines another variable called Coluna_13 as another list with a single value. In this case, 12.

This:

return {
    "Ct":Coluna_4,
    "Sa":Coluna_13,
}

You return a dictionary with two indexes, "Ct" and "Sa", the values of which are in a list of an element each. In this case:

{
    "Ct": [3],
    "Sa": [12]
}

This doesn’t make any sense:

CT=[Ct]
SA=[Sa]

The mistake happens because Ct obviously not defined in any part of the code. The error would occur in the bottom line for the same reason. Sa is also a variable that is not defined.

Browser other questions tagged

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