Easy question: More items in a For loop?

BJörn Lindqvist bjourne at gmail.com
Thu Apr 5 21:43:17 EDT 2007


> Here is some sample tuna:
> ['[7:55pm] <P0ke> My teachings goes back to the last iceage.\r\n',
> '[7:55pm] <%Zack> ahh now it does\r\n', '[7:55pm] <%Zack> ok\r\n',
> '[7:55pm] <P0ke> Or it is down just for you.\r\n', '[7:55pm] <@FC3>
> which one? that -12000 ice age or the one before\r\n', '[7:55pm]
> <P0ke> the earliest..\r\n', '[7:56pm] <P0ke> so.. 12000 quite long..\r
> \n', '[7:56pm] <@FC3> the one created by the meteor then\r\n',
> '[7:57pm] <P0ke> did not know that.. this is just a new teory I am
> folding.\r\n', '[7:57pm] * P0ke test test test\r\n']

You use the split method:

for j in tuna:
    time, text = j.split(' ', 1)
    if text.startswith('<%'):
        print 'Will be yellow'
    elif text.startswith('<@'):
        print 'Will be pink'
    elif text.startswith('*'):
        print 'Will be dark pink'

First each line in tuna is looped through. Then you split the line
into two pieces so that it becomes easier to manage. Then you just
check if the line begins with the searched after pattern. If it does,
you color it appropriately.

-- 
mvh Björn



More information about the Python-list mailing list