Obtaining data from excel and transforming into list, by Python

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 little script, in it I try to search the values of a column in a page in excel, so I found the logic good, but it says that the "Ct" is not defined, so how do I define this "Ct"?

    
asked by anonymous 11.11.2015 / 17:49

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 changes. In the function declaration:

def ler_arquivo(teste_termal1):

You do not use the variable teste_termal1 , so I removed it.

Here:

Coluna_4=[3]
Coluna_13=[12]

Variables in Python do not usually follow this initialization convention by uppercase. Since they are of no use, I also removed them and changed them to a dictionary, which I assume was the initial goal.

This here:

Coluna_4=[3]

Defines a Coluna_4 variable 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 being 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" , whose values are from a list of one element each. In case:

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

This does not make sense at all:

CT=[Ct]
SA=[Sa]

The error happens because Ct is obviously not defined anywhere in the code. The mistake would happen on the bottom line for the same reason. Sa is also a variable that is not defined.

    
12.11.2015 / 15:04