The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Parsing output
Discuss Parsing output in the Python Programming forum on Dev Shed. Parsing output Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 30th, 2004, 06:46 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation 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'"
|

January 30th, 2004, 07:04 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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
|

January 30th, 2004, 07:18 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation 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
|

January 30th, 2004, 07:31 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

January 30th, 2004, 08:24 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation 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
|

January 31st, 2004, 04:11 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

January 31st, 2004, 08:46 AM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thanks alot Mark! 
|

January 31st, 2004, 08:55 AM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
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.
|

January 31st, 2004, 11:41 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

January 31st, 2004, 12:53 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Nice work netytan
btw, where did you learn the 'PRIVMSG', 'JOIN', etc stuff for your bot?
|

January 31st, 2004, 12:53 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
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".
|

January 31st, 2004, 01:10 PM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation 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.
|

January 31st, 2004, 05:03 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

January 31st, 2004, 05:28 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
|
what is the '.' at the beginning of the message for?
|

January 31st, 2004, 05:37 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|