newbie problem

Manuel Hendel manuel at hendel.net
Tue Aug 27 20:06:00 CEST 2002


On Tue, Aug 27, 2002 at 05:10:03PM +0000, Frank Buss wrote:
> Perhaps I can help you. I'm a Python-newbie, too, so it is not the best 
> Python code, I think, but it works:

That's it, thank you very much, it looks realy easy, I always thought
about regualr expressions. Let me see if I understand what you've
done.
> 
> #!/usr/bin/python

Import of the split function from the string module
> from string import split

Import of the sys module
> import sys

Read in the whole file, line by line
> for line in open('in.txt', 'r').readlines():

Split every line in fields, but how does python know where to split??
>   fields = split(line)

If the field is empty, go on
>   if len(fields) == 0: continue

If the first field is a '#'
>   if fields[0] == '#':

Save the last field in the variable 'lastNumber'
>     lastNumber = fields[len(fields) - 1]

Else write the value of the 'lastNumber' to stdout
>   else:
>     sys.stdout.write(lastNumber)

I don't understand this 
>     for field in fields:
>       sys.stdout.write('|' + field)
>     sys.stdout.write('\n')
> 
> Send any questions to this newsgroup, if you don't understand something.
> 
> BW: Any experts who can improve it more Python-like?

Manuel

-- 
I distrust a man who says when. If he's got to be careful not to drink too 
much, it's because he's not to be trusted when he does. 
-Sidney Greenstreet, "The Maltese Falcon" 




More information about the Python-list mailing list