How to Iterate through cur.fetchall() in Python

前端 未结 5 920
刺人心
刺人心 2021-01-02 18:18

I am working on database connectivity in Python 3.4. There are two columns in my database.

Below is the query which gives me all the data from two columns in shown f

相关标签:
5条回答
  • 2021-01-02 18:41

    As the output you given [('F:\\test1.py', '12345abc'), ('F:\\test2.py', 'avcr123')]

    for i, row in data:
        print(i)  # i is column1's value
        print(row)# row is column's value
    

    So you don't need row[i] or row[j], that was wrong, in that each step of that iteration

    for i, row in data 
    

    is the same as i, row = ('abc', 'def') it set abc to variable i and 'def' to row

    BTW ,I don't know what database you use, if you use Mysql and python driverMySQL Connector, you can checkout this guide to fetch mysql result as dictionary you can get a dict in iteration, and the keys is your table fields' name. I think this method is convenient more.

    0 讨论(0)
  • 2021-01-02 18:44

    To iterate over and print rows from cursor.fetchall() you'll just want to do:

    for row in data:
        print row
    

    You should also be able to access indices of the row, such as row[0], row[1], iirc.

    Of course, instead of printing the row, you can manipulate that row's data however you need. Imagine the cursor as a set of rows/records (that's pretty much all it is).

    0 讨论(0)
  • 2021-01-02 18:46

    looking at

    [('F:\\test1.py', '12345abc'), ('F:\\test2.py', 'avcr123')]
      i              j                 i            j
    

    you are taking a strings i and j and indexing it like

       print(row['F:\\test1.py'])
        print(row['12345abc'])
    

    which gave you typeError

    TypeError: string indices must be integers
    

    this is because i in data is a string and your a indexing this

    try this

    for i,j in data:
        print(i)
        print(j)
    
    0 讨论(0)
  • 2021-01-02 18:53

    To iterate through this: [('F:\test1.py', '12345abc'), ('F:\test2.py', 'avcr123')]

    Code:
    for i in data:
       print i[0] + '\t' + i[1]
    

    Output:

    F:\test1.py 12345abc
    F:\test2.py avcr123

    0 讨论(0)
  • 2021-01-02 19:03

    It looks like you have two colunms in the table, so each row will contain two elements.

    It is easiest to iterate through them this way:

    for column1, column2 in data:
    

    That is the same as:

    for row in data:
        column1, column2 = row
    

    You could also, as you tried:

    for row in data:
        print row[0] # or row[i]
        print row[1] # or row[j]
    

    But that failed because you overwrote i with the value of first column, in this line: for i, row in data:.

    EDIT

    BTW, in general, you will never need this pattern in Python:

    i = 0
    for ...:
        ...
        i += 1
    

    Instead of that, it is common to do simply:

    for item in container:
        # use item
    
    # or, if you really need i:
    for i, item in enumerate(container):
        # use i and item
    
    0 讨论(0)
提交回复
热议问题