[Tutor] Alternative File I/O for Tuples (fwd)
kent37 at tds.net
Wed Jun 29 12:38:36 CEST 2005
Don Parris wrote:
> On Tue, 28 Jun 2005 05:50:54 -0400
> Kent Johnson <kent37 at tds.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,
>>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))
> 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 conditional. For example if I write
for i in range(3):
the output is
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):
the output is
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:
> 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?
> 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
> ### 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=' |'))
'Global name "rows" is not defined means that Python doesn't know what 'rows' means - you haven'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 just 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
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 trapped 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
More information about the Tutor