2016-12-10 28 views
1

我正在使用Raspberry Pi和tweepy基于鸣叫控制Pi上的GPIO引脚。例如,如果我的推文显示“正在加热”,则会启动一个继电器并开启加热功能。比较关键字与鸣叫gpio激活

我已经设法读取我的推文并将其打印出来,但我似乎无法实现的是将关键词与推文进行比较,然后执行某些操作。任何人都可以指引我走向正确的方向吗?

这是我到目前为止已经试过:

def on_status(self, status) 
    print (status.text) 
    tweet_check = unicode(status.text) 
    print tweet_check 
    if tweet_check == "heating on": 
     print "got it" # just to confirm capture 

def on_error(self, status): 
    print status 
+0

这是我使用高清on_status(个体经营,状态)的代码: 打印(status.text) tweet_check =的Unicode(status.text) 打印tweet_check 如果tweet_check == “猫”: 打印“了它“ 返回True –

+1

请将您的代码添加到您的文章中,而不是在评论中。我建议你看看[tour](http://stackoverflow.com/tour)。 –

+0

对不起julien lopez –

回答

0

您可以使用正则表达式匹配的对象关键字:

$tweet = 'Please, turn on the heating for me...'; 
$regex = '/\sheating\s/i'; 

preg_match($regex, $tweet, $matches); 

print_r($matches); 

应该打印出来:Array([0] => heating)

所以$matches数组将捕获任何匹配。 查看更多参考PHP文档。

+0

这将如何与我的代码工作,对不起我的无知挣扎在这里找到我的脚 –

+0

它将工作就像发布PHP问题,然后编辑它包括Python代码。虽然,你仍然能够在Python中使用正则表达式 –

+0

我发布在错误的部分,因为我没有看到任何暗示我使用php? -mister spock –