Thread: Parsing output

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    Parsing output


    Is there any way for me to parse this output

    Code:
    chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello
    to get the first word, and everything after it? For example, in the end "arg[0] == .say" and "arg[1] == 'Hello'"
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Can you mark in bold the bits you want? Not sure exactly waht you want here although i can tell you it's not gonna be very hard.

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Hehe I was hoping it wouldn't be, but I'm a real noob with regex (that's what I tried).

    Code:
    chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello
    I basicaly need the ".say" in arg[0] (or any other command it may be) and then the argument(s) in arg[1], arg[2], etc. In this case it would be "Hello" in arg[1]. Use another storage method if you know a better one

    Thanks in advance
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok here it is, this may yet need to be changed depending on format of other commands but it works perfectly on this...

    Code:
    >>> parse = 'chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
    >>> index = parse.find(':')
    >>> value = parse[index + 1:]
    >>> command, message = value.split()
    >>> command
    '.say'
    >>> message
    'Hello'
    >>>
    This is because it works by finding the first ':' char and using it as an index; since i don't know enough about IRC commands format.

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Ahhhh! I forgot a ':' at the beginning of the string... sorry about that? I hope it isn't hard to fix.

    It all seems to look like this

    Code:
    :chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Naw not hard, just needs a different aproch

    Code:
    >>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
    >>> value = parse.split(':')
    >>> value = value[-1]
    >>> command, message = value.split()
    >>> command
    '.say'
    >>> message
    'Hello'
    >>>
    Note: This should work, and with any similar format (string:.command value).

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Thanks alot Mark!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    11
    Another method to remember when searching through strings is that you can simplify a lot of problems if you reverse the string, then search, then reverse the result. This time, it's easy to do without; other times, it's invaluable.
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Perc, why not just use the rfind() string-method to search the string from the right?

    Code:
    >>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
    >>> index = parse.rfind(':') + 1
    >>> value = parse[index:]
    >>> command, message = value.split()
    >>> command
    '.say'
    >>> message
    'Hello'
    >>>
    It seems so much simpler than doing...

    Code:
    >>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello'
    >>> value = parse[::-1]
    >>> index = value.find(':')
    >>> value = value[:index]
    >>> value = value[::-1]
    >>> command, message = value.split()
    >>> command
    '.say'
    >>> message
    'Hello'
    >>>
    ...like you're recomending. So no need to manually reverse the string or results

    Mark.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Nice work netytan

    btw, where did you learn the 'PRIVMSG', 'JOIN', etc stuff for your bot?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Except that searching for the first ":" from the right isn't the right way to parse IRC messages

    First, a quick caveat. I know you're just playing around with this stuff to learn python, and that's cool and all. But, please realize that there are good IRC libraries out there that do this, so don't agonize over making a new one that does all this

    Okay, back to the topic at hand. You'll want to read the IRC RFC (1763, I think) to figure out the message structure. But the rightmost colon isn't necessarily what you are looking for. This is a valid message, for example:

    Code:
    :chris_!~chris@bleh.net PRIVMSG #thisisatest :This is a valid message: foo
    And " foo" isn't the message you want, you want "This is a valid message: foo".
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Hmm.. well that complicated things

    I'm not planning this to be a complicated bot so I want to try and write it without other libs. I just need the parser working right.

    By the way, wouldn't this work?

    Code:
    >>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello:Hi'
    >>> index = parse.rfind(':.') + 1
    >>> value = parse[index:]
    >>> value = value.split()
    >>> value             
    ['.say', 'Hello:Hi']
    (netytan's code)

    Then, say the command was ".say" I could do something like this:

    Code:
    >>> command = value[0]
    >>> text = ' '.join(value[1:])
    >>> command
    '.say'
    >>> text
    'Hello:Hi'
    Last edited by XxChris; January 31st, 2004 at 01:22 PM.
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    To be honest i know nothing about IRC commands; I was talking to Xlord on yahoo and he was explaining his functions so i just asked him some questions and wrote the IRC class for him

    Anyway just thought i'd mention that you can loose the whole join() method call etc by telling split() on the first space...

    Code:
    >>> parse = ':chris_!~chris@bleh.net PRIVMSG #thisisatest :.say Hello:Hi baby'
    >>> index = parse.rfind(':.') + 1
    >>> value = parse[index:]
    >>> command, message = value.split(' ', 1)
    >>> comamnd
    '.say'
    >>> message
    'Hello:Hi baby'
    >>>
    Nothing wrong with rewriting things - i do it all the time - its a good way to learn. Plus, sometimes you end up with a better version than whats already out there. I mean.. would you have the IRC libs you have today if everyone just stuck with the first one?

    Mark.
    programming language development: www.netytan.com Hula

  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    what is the '.' at the beginning of the message for?
  28. #15
  29. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Mmmm there isnt a '.' at the beginning of 'message', there is one at the beginning of 'command' which i assumed had something to do with IRC. In any case Chris wanted it in a variable so thats how i parsed it

    Mark.
    programming language development: www.netytan.com Hula

Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo