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 29 2005 3:39AM 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:
>  On Tue, 28 Jun 2005 05:50:54 -0400
>  Kent Johnson <kent37@[...].net> wrote:
> >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()
> >
>  
>  
>  O.k., I'm curious about the indentation issue here.  

Python uses indentation to show the scope of a block - how much code is inside a loop or con
ditional. For example if I write
for i in range(3):
  print i
  print 'Done'

the output is

0
Done
1
Done
2
Done

The two print statements are both indented under the for statement so they are both part of 
the loop set up by the for statement. OTOH if I say

for i in range(3):
  print i
print 'Done'

the output is
0
1
2
Done

because the second print is at the same indentation as the for so it is not included in the 
loop. This section of the tutorial talks a little about indenting:
http://docs.python.org/tut/node5.html#SECTION005200000000000000000

>  There is only one file,
>  and it has the correct information - looking at it from a text editor.  Is
>  my version overwriting the file every time it iterates then?  

Yes, exactly.

>  Here we go.  Global name "rows" is not defined.  My guess is that "rows"
>  needs to know that it should be holding "Results", but maybe it should
>  already know that.  I tried to say rows = Results, but that did not help.
>  This seems to come back to the difficulty I have with passing arguments
>  around.
>  
>  ### Revised Code ###
>      print 'Phone List'
>      for record in Results:
>          print '%-15s\t%-15s\t%-15s' % record
>      # rows = Results did not work
>      mbrPhone = open('mbrPhone.txt', 'w')
>      mbrPhone.write(indent(rows, hasHeader=False, separateRows=False,
>                                prefix='| ', postfix=' |'))
>      mbrPhone.close()

'Global name "rows" is not defined means that Python doesn't know what 'rows' means - you ha
ven't assigned any value to that name. I'm surprised your code didn't work when you add the 
line "rows = Results" as indicated above; what error do you get then? But a simpler way is j
ust to use Results in the call to indent:

    mbrPhone.write(indent(Results, hasHeader=False, separateRows=False,
                              prefix='| ', postfix=' |'))

What happens if you try that?

>  BTW, I really appreciate your patience and willingness to help me understand
>  this.

No problem, that's what we do here. At least on a good day :-)

>  If I had ever dreamed that I would have a desire to program 20 years
>  after the fact, I would have stopped passing notes in Math class.  I do it
>  well, but hate it.  Yet, I find myself drawn further and further into the
>  code, actually wanting to know more about it - why it does what it does.

I don't know about the 'hating it' part, but you are certainly not alone in finding yourself
 fascinated with programming in Python. But snakes can do that, can't they? We are all trapp
ed by its hypnotic stare... ;-)

BTW have you found a tutorial you like? There are many free Python tutorials, take a look at
 this page: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

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