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.
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 (thefor
and theprint
)? Question 2: What you wished to do while usingCT=[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)– Luiz Vieira
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 element12
: 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]
?– Luiz Vieira
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.
– Mauricio Gabriel
It follows from where I tried to get this code Link
– Mauricio Gabriel