newbie problem

Frank Buss fb at frank-buss.de
Tue Aug 27 15:02:18 EDT 2002


Manuel Hendel <manuel at hendel.net> wrote:

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

More object oriented: fields = line.split()

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

You mean the fields list.

> I don't understand this 
>>     for field in fields:
>>       sys.stdout.write('|' + field)
>>     sys.stdout.write('\n')

Iterate over all elements in the fields list (in every iteration the next 
element will be assigned to the field variable). For every iteration, 
write '|', concatenated with the current field.

I used 'write', because 'print' added a newline. More object oriented and 
Python-like would be something like the solution from Matt:

print '|'.join([lastNumber]+fields)

The argument within the 'join' method call is a list. 'lastNumber' is 
packed in a list by surrounding it with [] and this list is concatenated 
with the fields list. The result list is passed to the join-method. This 
is a method of the String class. The method is called on the '|' instance 
of the string class, which joins the elements of the list and adds the 
string itself between the elements.

-- 
Frank Buß, fb at frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de



More information about the Python-list mailing list