PYTHON - FOR inside FOR

Asked

Viewed 2,009 times

0

I’m performing a select on DB with the Python and treating this return. My doubt is in the use of a for within other’s for, use the script below:

import mysql.connector

mydb = mysql.connector.connect(
                                user='root',
                                host='localhost',
                                password='***')

mycursor = mydb.cursor()

mycursor.execute("select calldate, clid, src, dst from cdr.cdr \
where calldate like '2019-02-18%'\
ORDER BY calldate desc limit 2")

myresult = mycursor.fetchall()

for row in myresult:
    for fild in row:
        print (fild[0])
        print (fild[1])
        print (fild[2])
        print (fild[3])

mydb.close()

And the following error is being displayed:

# python3.6 dumpMysql.py
Traceback (most recent call last):
  File "dumpMysql.py", line 18, in <module>
    print (fild[0])
TypeError: 'datetime.datetime' object is not subscriptable

I’m trying to get the output be it:

Primeira linha do select
campo0
campo1
campo2
campo3

Segunda linha do select
campo0
campo1
campo2
campo3
  • When you run the code, what is inside the "myresult" variable? What kind of object is allocated to it?

2 answers

0

To get this result, write the iteration like this:

for row in myresult:
    for fild in row:
        print (fild)

Why? Your code gets every row contained in myresult and then in each row he catches a fild at a time. For each fild that he picks up, makes a print(fild) onscreen.

You don’t have to put fild[0]...fild[3], only fild even.

  • When I made your suggestion the output was something close to what I need, but the use of Fild[1]...Fild[3], is why I need to treat the returned fields.

  • You say treat the data after displaying?

  • I need to return Fild[0] to calldate, Fild[1] to clid, Fild[2] to src and Fild[3] to dst

  • Okay, I’ll modify it here so you can have access to the files later.

  • Basically I need to play each Fild in a variable and then treat... The idea is to throw everything into a mongoDB.

  • You don’t actually need to change the answer. The thing is this. You already have all the Ilds in the myresult variable. You can access them by calling something like myresult[0][0], for example. Doing myresult[0][0], you take the first Fild of the first Row.

  • When you put Fild[0], Fild[1], Fild[2], Fild[3], you are actually accessing a position within the Fild object that is being treated at the moment. Pra conseguir o que você deseja, tem que acessar é myresult[0][0], myresult[0][1], myresult[0][2] e myresult[0][3] para os filds da primeira row, myresult[1][0], myresult[1][1], myresult[1][2] e myresult[1][3] para os filds da segunda row, e assim por diante...

  • Working with the variety "myresult", has opened my mind, but what can I do, when I do not know the line size of the variable "myresult" In this example I am limiting the query to 2 results, but in production I will select for a whole day, this will give many lines.

  • You can tell how big the myresult is using Len(myresult). This Len() gives you the length/size (length) of an object. With Len(myresult) you will know how many lines have your result. :)

Show 4 more comments

0


By the comments you need to "save" the Fields in variables for further treatment, so the suggestion is to store these Fields in a dictionary, actually in a list of dictionaries (a dictionary for each Row):

lst_dicts = []
for row in myresult:
    d = {}
    d['calldate'] = row[0] 
    d['clid'] = row[1] 
    d['src'] = row[2] 
    d['dst'] = row[3] 
    lst_dicts.append(d)

At the end you will have a list of dictionaries, each with 4 keys/value, represented the names of the fields and their respective values within each line in myresult.

Browser other questions tagged

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