Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.02; 'assign': 0.07; 'assigning': 0.09; 'cursor': 0.09; 'pyodbc': 0.09; 'cc:addr :python-list': 0.11; 'sake,': 0.16; 'statments': 0.16; 'tuple': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'help.': 0.21; '>>>': 0.22; 'select': 0.22; 'programming': 0.22; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; 'connected': 0.24; 'mon,': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'sep': 0.31; 'lists': 0.32; 'url:python': 0.33; 'basic': 0.35; 'subject:lists': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'url:listinfo': 0.36; 'doing': 0.36; 'url:org': 0.36; 'list': 0.37; 'server': 0.38; 'follows:': 0.38; 'skip:m 40': 0.38; 'skip:p 20': 0.39; 'url:mail': 0.40; 'hope': 0.61; 'new': 0.61; '30,': 0.65; 'to:addr:gmail.com': 0.65; 'records': 0.73; '.....': 0.78; '2014,': 0.84; 'jerome': 0.91; 'joel': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=qvq9veMcG4Gl8+baKp7BZ0J4kaAnMvsigXdz4XMg2Mk=; b=VCYGFoGbp4l/R7orw6PJ1PaximE+Qg2XIxWGZ0cPNrkRL92UktU37lhnpjGasHaKBr 8jFP4TnymrLewp+59Je+Z/86YnhLDdyIqIQfH3/Q1RYxrC6dFvp8+viZU75S0aM2f+eg rWte9ei2h8nLM6LnogjVgcXDagS3JJGZATvLFqXKbBpDxW1X4VRAoHL4zF6O7ODMyXYI uXT/JqeKE7vEF0tEwm9fAkP/5XOxVHlZ4mFydk3yo9JrRllpea9n99vbO/boAutH9QXJ PZZwJMuN53l3ot6HmsatFxRZ0NNRZ2U536ClxrZgeFWJLYtNuHjLUxpWU090cuUW1luc iw6A== MIME-Version: 1.0 X-Received: by 10.52.118.73 with SMTP id kk9mr19053767vdb.13.1380553829887; Mon, 30 Sep 2013 08:10:29 -0700 (PDT) In-Reply-To: <812b0b0b-fa73-4fc3-b8d0-ac0a9c93207e@googlegroups.com> References: <812b0b0b-fa73-4fc3-b8d0-ac0a9c93207e@googlegroups.com> Date: Mon, 30 Sep 2013 11:10:29 -0400 Subject: Re: fetchall to python lists From: Joel Goldstick To: christensen.jerome@gmail.com Content-Type: multipart/alternative; boundary=089e0122f0f29fb96804e79b3cf5 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 146 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380553832 news.xs4all.nl 15867 [2001:888:2000:d::a6]:59414 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55085 --089e0122f0f29fb96804e79b3cf5 Content-Type: text/plain; charset=UTF-8 On Mon, Sep 30, 2013 at 10:58 AM, wrote: > Hi - I have some basic programming experience and new to Python. I have > connected to SQL Server as follows: > > import pyodbc > conn = pyodbc.connect('DSN=DBC') > cursor = conn.cursor() > cursor.execute("select measure,fin_year_no,fin_week_no,location_no,value > from actual") > result=cursor.fetchall() > > result looks like this: > > > > result[0] - ('2013', 2014, 7, 242, 96064.35) > result[1] - ('2013', 2014, 7, 502, 18444.2) > .... approximately 2m records > > Is there a way to assign the values of result to 5 lists without doing 5 > select statments one for each of the colums and then assigning it to a list > so that: > > What you have below is just result[0][0], result[0][1], etc. list1[0] = '2013' > list1[1] = 2014 > list1[2] = 7 > list1[3] = 242 > list1[4] = 96064.35 > > list2[0] = '2013' > list2[1] = 2014 > list2[2] = 7 > list2[3] = 502 > list2[4] = 18444.2 > > and so on ... > > Hope someone can help. Regards Jerome > So what I'm trying to say is that you already have what you want. each tuple is contained in the out list of all of the tuples. For brevity sake, I am acting as if the data set contained only a single row: >>> result = (('2013', 2014, 7, 242, 96064.35),) >>> result (('2013', 2014, 7, 242, 96064.35),) >>> result[0] ('2013', 2014, 7, 242, 96064.35) >>> result[0][0] '2013' >>> result[0][1] 2014 -- > https://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick http://joelgoldstick.com --089e0122f0f29fb96804e79b3cf5 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



On Mon, Sep 30, 2013 at 10:58 AM, <christensen.jerome@= gmail.com> wrote:
Hi - I have some basic pr= ogramming experience and new to Python. I have connected to SQL Server as f= ollows:

import pyodbc
conn =3D pyodbc.connect('DSN=3DDBC')
cursor =3D conn.cursor()
cursor.execute("select measure,fin_year_no,fin_week_no,location_no,val= ue from actual")
result=3Dcursor.fetchall()

result looks like this:



result[0] - ('2013', 2014, 7, 242, 96064.35)
result[1] - ('2013', 2014, 7, 502, 18444.2)
.... approximately 2m records

Is there a way to assign the values of result to 5 lists without doing 5 se= lect statments one for each of the colums and then assigning it to a list s= o that:


What you have below is just result[0][= 0], result[0][1], etc.


list1[0] =3D '2013'
list1[1] =3D 2014
list1[2] =3D 7
list1[3] =3D 242
list1[4] =3D 96064.35

list2[0] =3D '2013'
list2[1] =3D 2014
list2[2] =3D 7
list2[3] =3D 502
list2[4] =3D 18444.2

and so on ...

Hope someone can help. Regards Jerome

S= o what I'm trying to say is that you already have what you want.=C2=A0 = each tuple is contained in the out list of all of the tuples.

For b= revity sake, I am acting as if the data set contained only a single row:
>>> result =3D=C2=A0 (('2013', 2014, 7, 242, 96064.35)= ,)
>>> result
(('2013', 2014, 7, 242, 96064.35),)>>> result[0]
('2013', 2014, 7, 242, 96064.35)
>= >> result[0][0]
'2013'
>>> result[0][1]
2014

--
https://mail.python.org/mailman/listinfo/python-list



--
--089e0122f0f29fb96804e79b3cf5--