2

I want to know how to allow multiple inputs in Python.
Ex: If a message is "!comment postid customcomment"
I want to be able to take that post ID, put that somewhere, and then the customcomment, and put that somewhere else.
Here's my code:

import fb
token="access_token_here"
facebook=fb.graph.api(token)

#__________ Later on in the code: __________

                elif msg.startswith('!comment '):
                    postid = msg.replace('!comment ','',1)
                    send('Commenting...')
                    facebook.publish(cat="comments", id=postid, message="customcomment")
                    send('Commented!')

I can't seem to figure it out.
Thank you in advanced.

Andbdrew
  • 11,788
  • 4
  • 33
  • 37
Axiom
  • 902
  • 1
  • 10
  • 23

1 Answers1

2

I can't quite tell what you are asking but it seems that this will do what you want.
Assuming that msg = "!comment postid customcomment" you can use the built-in string method split to turn the string into a list of strings, using " " as a separator and a maximum number of splits of 2:

msg_list=msg.split(" ",2)

the zeroth index will contain "!comment" so you can ignore it

postid=msg_list[1] or postid=int(msg_list[1]) if you need a numerical input

message = msg_list[2]

If you don't limit split and just use the default behavior (ie msg_list=msg.split()), you would have to rejoin the rest of the strings separated by spaces. To do so you can use the built-in string method join which does just that:

message=" ".join(msg_list[2:])

and finally

facebook.publish(cat="comments", id=postid, message=message)

mmdanziger
  • 4,466
  • 2
  • 31
  • 47
  • Well, i'd need it to remove "!comment " and then put the post ID in the id section, and then the message in the message section. Meaning: !comment 987654321 Hello It'd comment on the post number 987654321 saying "Hello". That's why I use ` postid = msg.replace('!comment ','',1) ` I had it to where it posted a generic comment, if the user put in the post ID. What came after "!comment " was the post ID. It replaced the "!comment " with nothing, thus only leaving the postID, thus the reason why I did "id=postid" Sorry if this sounds confusing. I haven't slept from trying to figure this out – Axiom Jan 13 '14 at 14:42
  • But what does the full `msg` string look like? – mmdanziger Jan 13 '14 at 14:47
  • `msg = self.Message.Body` -- Basically, if message is received. This is a Skype bot I'm working on. So, if I get a message from someone saying "!comment 234567 This is a comment" I want it to be able to recognize which is the post ID and which is the custom comment. – Axiom Jan 13 '14 at 14:48
  • ok, i'll rephrase it. what does `print msg` or in py3k `print(msg)` look like? – mmdanziger Jan 13 '14 at 14:48
  • Yeah, I fixed my comment. Sorry. Basically, to make this an easier question. Let's say I had a string that was "Cats like yarn" How would I be able to break down the string into 3? Meaning one being "cats" one being "like" and one being "Yarn" so I can later do something like ` print word1, word3 ` – Axiom Jan 13 '14 at 14:54
  • Of course the string wouldn't be fixed. It'd be custom. Ex: I can type a string and it'll break it down for me. So it won't necessarily be "cats like yarn" – Axiom Jan 13 '14 at 14:56
  • split and join should do everything you need, I edited my answer to make it more clear. – mmdanziger Jan 13 '14 at 18:56
  • 2
    ``split`` takes a couple of optional arguments: (1) the character(s) on which to split the string, and (2) the maximum number of splits. So if you say ``msg.split(' ', 2)`` -- you get three strings back. You don't have to join all the remaining strings back together again. – Wolf Jan 13 '14 at 19:31