Easy question: More items in a For loop?

A

Adam W.

I'm trying to write a script that will parse IRC chat logs and color
code them if it finds certain characters. I was able to make this
work with one character, but to make it even more accurate I would
like to use two identifying characters. Here is my code :

import urllib2

response = urllib2.urlopen("http://192.168.1.100:81/#pi.log")

tuna = response.readlines()[-10:]

for j in tuna:
for e,n in j:
if e,n == "*"," ":
j = "This: " + str.strip(j) + " will
be Pink"
elif e,n == "<","%":
j = "This: " + str.strip(j) + " will
be yellow"
elif e,n == "<","@":
j = "This: " + str.strip(j) + " will
be dark pink"

print(str.strip(j))

Obviously the "for e,n" business doesnt work, but I think it makes for
some decent pseudocode for what I'm trying to accomplish.

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']
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top