[Tutor] Introduction - log exercise

Dave Angel davea at ieee.org
Wed Nov 18 03:28:58 CET 2009



Antonio de la Fuente wrote:
> * Dave Angel <davea at ieee.org> [2009-11-17 16:30:43 -0500]:
>
>   
>> <snip>
>>     
> for line in fileIn:                                                                                      
>     if line.isspace():                                                                                   
>         print "***** blank line ****"                                                                    
>         print myList                                                                                     
> 	print "***** fancy blank line ****"
>         myList =]                                                                                      
>     else:                                                                                                
>         myList.append(line)       
>
> I think is what i expect, but confuse me that is in this format:
>
> ['Tue Nov 17 16:11:47 GMT 2009\n'], '\tbladi bladi bla', '\ttarila ri la\n', '\tpatatin pataton\n', '\ttatati tatata\n', '\tfoo\n']
> ***** fancy blank line ****
> ***** blank line ****
>
> with linefeeds and tabs all over, I see why everybody calls it
> paragraph.
> Once I write to a file from the list, it will comeback the initial
> format of the file?
>
>   
No, when you want to write it to the file, you'll need to loop through 
the list.  There is a shortcut, however.  If you simply do:
        stdout.write(  "".join(myList)  )

it will join all the strings in the list together into one big string 
and then send it to stdout.  You can do that because each line still has 
its newline at the end.

BTW, the list you quoted above is malformed.  You really need to 
copy&paste code and data into the message, so information isn't lost.  
You have an extra right bracket and a missing \n in there.


>> Next version of the code: whenever you have a non-blank line, in
>> addition to adding it to the list, also check it for whether it's
>> equal-foo.
>> If so, set a flag.  When printing the outlist, skip the printing if
>> the flag is set.  Remember that you'll have to clear this flag each
>> time you blank
>> the mylist, both before the loop, and in the middle of the loop.
>>     
>
> I am a bit lost with the flags, is it what Bob Gailer was calling keep True, keep = False, right?
>
>   
You can certainly call it 'keep'   The point is, it'll tell you whether 
to output a particular paragraph or not.
>> Once this makes sense, you can worry about actually writing the
>> output to a real file, maybe compressing it, maybe doing deletes and
>> renames
>> as appropriate.   You probably don't need shutil module,  os module
>> probably has enough functions for this.
>>
>> At any of these stages, if you get stuck, call for help.  But your
>> code will be only as complex as that stage needs, so we can find one
>> bug at a time.
>>
>> DaveA
>>
>>     
> Thank you, it has been very helpful.
>
>   
You're very welcome.

DaveA


More information about the Tutor mailing list