ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Alternative File I/O for Tuples (fwd)
by Kent Johnson other posts by this author
Jun 28 2005 2:52AM messages near this date
Re: [Tutor] Alternative File I/O for Tuples (fwd) | Re: [Tutor] Alternative File I/O for Tuples (fwd)
Don Parris wrote:
>  Just getting back to this - Mondays are always hectic.  This recipe is the
>  one I saw and like.  It looks cool!  In my brief efforts tinkering with it,
>  I am not really getting very far.  I saved the recipe, and import it into
>  the file containing all my database functions.  In one of the
>  functions, I have the following:
>  
>  I used the "from tbl_Tabs import *" approach to import the recipe.  If I
>  should be doing this differently, just say so.
>  
>      for record in Results:
>          print '%-15s\t%-15s\t%-15s' % record    
>  # I still want to print to screen, then...
>  
>          mbrPhone = open('mbrPhone.txt', 'w')
>          mbrPhone.write(indent(record, hasHeader=False, separateRows=True))
>          mbrPhone.close()

The problem is you are just passing one record to indent(). It processes the whole table at 
once so you have to pass the list of records, i.e.
        mbrPhone.write(indent(Results, hasHeader=False, separateRows=True))

and do this outside the loop or you will write the file once for every record. So your code 
should look like this:

    for record in Results:
        print '%-15s\t%-15s\t%-15s' % record    
# I still want to print to screen, then...

    # Notice different indent from your code
    mbrPhone = open('mbrPhone.txt', 'w')
    mbrPhone.write(indent(Results, hasHeader=False, separateRows=True))
    mbrPhone.close()

>  
>  I first assumed that "record", and then "Results" could be substituted for
>  "rows", per the recipe.  I also tried "rows", and assigning "record", and
>  then "Results" to "rows".  O.k., you can laugh all you want.  I tried
>  playing with the various arguments, and found that to be of little value.
>  
>  The traceback refers to a type error: iteration over a non-sequence.  The
>  query returns a tuple, as I understand it.  I also understand a tuple to be
>  a sequence.  If this is really as simple as the 3 lines you pointed out
>  above, I know I'm missing something in the implementation.

Yes, a tuple is a sequence. If you still get an error with the code above, please post the e
xact error message and traceback (copy / paste from the output), there is a lot of useful in
formation in it that can be lost if you paraphrase.

Kent

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Danny Yoo
Kent Johnson
Don Parris
Don Parris
Kent Johnson
Don Parris
Kent Johnson
Don Parris
Kent Johnson
Don Parris
Kent Johnson
Don Parris

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved