RE: Python Newbie with a Python/MYSQL Question

Posted on 2012-03-19
Last Modified: 2012-03-20
   I'm a Python newbie.  I have a pretty basic script that does a "select * from tablename" and returns two list of tuples.  The format of my TEST database looks like this:

mysql> select * from employee;
| id  | name   | dept       |
| 100 | Thomas | Sales      |   
| 200 | Jason  | Technology |  
| 300 | Mayla  | Technology |  

Open in new window

I want to have the column match the first record in the row.  For example, my first line would look like
id: 100 name: Thomas Dept: Sales
id:200 name: Jason  Dept: Techology
id: 300 name: Mayla Dept: Technology

Here's the last bock of my code with output:
         cursor.execute("SELECT * FROM" + ' ' + table)


         cfields=[i[0] for i in cursor.description]

         return cfields,data1


         print "Error: unable to fetch data"


def ColumnFields():

    return cfields

def main():


    print test1
    print test2

Open in new window

     Can someone please give me ideas on how to do this.  Also, for some reason, when I print this out, I get an extra letter in my output for the second tuple.  Why is this (the L shouldn't be there with the numbers)?  Also, why is my first output a list and not a tuple? Thanks in advance!!

['id', 'name', 'dept', ]
((100L, 'Thomas', 'Sales'), (200L, 'Jason', 'Technology'), (300L, 'Mayla', 'Technology',), (400L, 'Nisha', 'Marketing'), (500L, 'Randy', 'Technology'))
Question by:unix_admin777
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 12

Assisted Solution

larsrohr earned 100 total points
ID: 37740894
Still learning python myself, but maybe I can help somewhat.

The L says that these are long integers; you could convert them to plain integers before printing by using int(x).

I think the first output is a list because it's defined that way via the brackets here:  cfields=[i[0] for i in cursor.description]

The other output is tuples as expected.

Looking forward to follow-up comments by you and other experts...
LVL 29

Expert Comment

ID: 37741058
Yes, larsrohr deserves his points :)

In 100L the L simply means that the long integer type is used by Python.  You should not care about it.  The long integer and the "normal" integer were merged in newer versions of Python.  Think about it as about implementation detail.

He is right also with the list/tuple answer.  The cfields = [i[0] for i in cursor.description] means that you are assigning the result of so called list comprehension to the cfields.

The equivalent code without using the list comprehension is:

    cfields = []    # init -- empty list
    for i in cursor.description:

Open in new window

To dig deeper, the i[0] for i in cursor.description is a generator expression that returns iterator.  The iterator goes through all values of i[0] where i goes through all values from another iterator returned by the cursor.description.

Try cfields = tuple([i[0] for i in cursor.description) instead (i.e. feed the tuple constructor with the results of the same generator expression instead of enclosing in the square brackets).  In other words, the square brackets in the list comprehension could be viewed as a syntactic sugar for the cfields = list([i[0] for i in cursor.description)

A side note, the cursor.execute("SELECT * FROM" + ' ' + table) can be a bit simplified as cursor.execute('SELECT * FROM ' + table).  Single quotes and double quotes have exactly the same meaning -- you can choose.  There is no char type in Python 'x' is not a character like in the C language.  It is only a single-letter string.
LVL 29

Accepted Solution

pepr earned 400 total points
ID: 37741307
I did not noticed... Here is the code that shows how to format your extracted data:
def formatRecord(names, values):

    # There should be the same number of the elements in the input sequences.
    assert len(names) == len(values)

    # The built-in zip() function is capable to merge two or more sequences.
    # Here we get the mixture of field names and of its values.  Generally,
    # we may not know, how many of the elements are processed.  Let's do it
    # in the loop.  Let's form the list of 'name: value' strings.
    lst = []                                 # init
    for name, value in zip(names, values):   # could be   for t in zip(...)
        s = '%s: %s' % (name, value)         # could be     s = '%s: %s' % t
    # Now get the result via joining the list of substrints into one 
    # string.  (The lst intermediary step makes it usually more efficient
    # because strings in Python are immutable and each "modification of a string"
    # actually means "construction of a new string and throwing away the old one".
    return '\t'.join(lst)
if __name__ == '__main__':

    # Let's pretend we extracted the following.
    cfields = ('id', 'name', 'dept')
    data = ((100L, 'Thomas', 'Sales'),
            (200L, 'Jason', 'Technology'),
            (300L, 'Mayla', 'Technology',),
            (400L, 'Nisha', 'Marketing'),
            (500L, 'Randy', 'Technology')

    # Now loop through the data, form and print the output.
    for row in data:
        print formatRecord(cfields, row)

Open in new window

It prints on my console (fields separated by tabulator):

id: 100 name: Thomas    dept: Sales
id: 200 name: Jason     dept: Technology
id: 300 name: Mayla     dept: Technology
id: 400 name: Nisha     dept: Marketing
id: 500 name: Randy     dept: Technology

Open in new window


Author Closing Comment

ID: 37745299
Thanks for all of the help.  The explanations were excellent as well.

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp Google Map 2 59
Where to download python requests library 6 63
Understanding ping command in Ubuntu 5 30
powershell automatic variable for results 21 23
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question