Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Easy question: More items in a For loop?

Reply
Thread Tools

Easy question: More items in a For loop?

 
 
Adam W.
Guest
Posts: n/a
 
      04-06-2007
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/%23pi.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']

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=
Guest
Posts: n/a
 
      04-06-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it reasonably easy easy to something like this with python? Bruno Desthuilliers Python 5 08-29-2007 07:40 AM
Stripping audio from qa DVD. Easy or not so easy? GJ Computer Support 1 05-23-2007 02:03 AM
any easy way to do this?-put items in table =?Utf-8?B?UGF1bA==?= ASP .Net 3 06-04-2005 08:14 AM
easy to look at and easy to maintain web page menuing system. Hazzard ASP .Net 2 04-06-2004 03:51 AM



Advertisments