将关键字与推文进行比较以进行gpio激活

时间:2016-12-10 13:19:15

标签: python raspberry-pi tweepy

我正在使用Raspberry Pi和tweepy根据推文控制Pi上的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

1 个答案:

答案 0 :(得分: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文档。