Easy question: More items in a For loop?

Discussion in 'Python' started by Adam W., Apr 6, 2007.

  1. Adam W.

    Adam W. Guest

    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']
     
    Adam W., Apr 6, 2007
    #1
    1. Advertising

  2. > 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
     
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Apr 6, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hazzard
    Replies:
    2
    Views:
    677
    Hazzard
    Apr 6, 2004
  2. Michael
    Replies:
    4
    Views:
    466
    Matt Hammond
    Jun 26, 2006
  3. ds4ff1z
    Replies:
    4
    Views:
    410
    ds4ff1z
    Aug 9, 2006
  4. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    236
    Jeff Wood
    Sep 29, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    444
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page