I hope someone can give me a light with the assignment of a value depending on a tuple, I want to assign position 3 to my variable, but when doing so it tells me that it is out of range, my tuple comes out of a query in a base of data, which brings me 4 data, of these four I need the last one, which would be 3, counting 0.
I have tried to assign the result of this query in an array with the aim of seeing if it is because of this, however I still have the same problem.
This is the execution log:
Ad117
[('220008322401', '987654321', 'SOLICITANTE', 547)] #Este es el resultado de mi consulta
Traceback (most recent call last):
File "D:/adherencia_revision.py", line 1340, in <module>
Ejecutar()
File "D:/adherencia_revision.py", line 1309, in Ejecutar
Ad117()
File "D:/adherencia_revision.py", line 1038, in Ad117
print("bgAnterior[3] ",bgAnterior[3])
IndexError: list index out of range
This is my code:
try:
with open("D:/consulta.sql","r") as query:
sql = query.read()
with BDMicro() as consulta:
gAnterior = consulta.query(sql)
except Exception as err:
print(err)
bgAnterior = []
for row in gAnterior:
bgAnterior.append(row)
print(bgAnterior)
if gAnterior:
print("bgAnterior[3] ",bgAnterior[3])
anterior = bgAnterior[3]
print("anterior ",anterior)
if anterior in fondob:
if actual in fondoa:
res1 = (rowAd[0],rowAd[1],observ,accion)
res.append(res1)
According to the question, the result of the query remains in
bgAnterior
This is a list and it contains a single element, which is a 4-tuple.
To retrieve the tuple it is a matter of extracting the first element:
and from the tuple you can get the field you want using indices again:
It is not necessary to extract the tuple to access the element; you can combine both accesses into one
show
produces