2

Python で複数の入力を許可する方法を知りたいです。
例: メッセージが「!comment postid customcomment」の場合、
その投稿 ID を取得して、それをどこかに置き、次に customcomment を置き、それを別の場所に置きたいと考えています。
これが私のコードです:

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!')

私はそれを理解できないようです。
よろしくお願いします。

4

1 に答える 1

2

あなたが何を求めているのかよくわかりませんが、これはあなたが望むことをするようです。組み込みの文字列メソッドを使用して文字列を文字列のリストに変換 できる
と仮定すると 、セパレータとして使用し、分割の最大数は 2 です。msg = "!comment postid customcomment"split" "

msg_list=msg.split(" ",2)

ゼロ番目のインデックスには「!comment」が含まれるため、無視できます

postid=msg_list[1]またはpostid=int(msg_list[1])数値入力が必要な場合

message = msg_list[2]

分割を制限せずにデフォルトの動作 (つまりmsg_list=msg.split()) を使用する場合、スペースで区切られた残りの文字列を再結合する必要があります。これを行うには、組み込みの string メソッドjoinを使用できます。

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

そして最後に

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

于 2014-01-13T14:16:45.433 に答える