简体   繁体   English

AttributeError:'NoneType'对象没有属性'group'

[英]AttributeError: 'NoneType' object has no attribute 'group'

please help as i am trying to use a raspberry pi pir sensor to transfer the data collected by the pir sensor(which is 1 or 0 ) to the web service and i got this error 请帮忙,因为我尝试使用树莓派Pir传感器将Pir传感器(为1或0)收集的数据传输到Web服务,但出现此错误

Traceback (most recent call last):
  File "pir_5.py", line 54, in <module>
    moveHold = float(matches.group(1))
AttributeError: 'NoneType' object has no attribute 'group'

and this is my code 这是我的代码

while True :

    # Read PIR state
    Current_State = GPIO.input(GPIO_PIR)

    if Current_State==1 and Previous_State==0:
      # PIR is triggered
      output =  subprocess.check_output(["echo", "18"]);
      print "  Motion detected!"
      # Tell the Pi to run our speech script and speak the words
      # motion dtected! - anything after the .sh will be read out.
      matches = re.search("Current_State==1 and Previous_State==0", output)
      moveHold = float(matches.group(1))
      resultm = client.service.retrieveMove(moveHold)

      cmd_string = './speech.sh motion detected!'
      # now run the command on the Pi.
      os.system(cmd_string)
      # Record previous state
      Previous_State=1
    elif Current_State==0 and Previous_State==1:
      # PIR has returned to ready state
      print "  Ready"
      Previous_State=0

    # Wait for 10 milliseconds
    time.sleep(0.01)

Then obviously the output does not contain the expected string. 那么显然output中不包含预期的字符串。 (How should it, when it is generated by calling echo 18 ?) Thus, (当通过调用echo 18生成它时,该怎么办?)

  matches = re.search("Current_State==1 and Previous_State==0", output)

returns None , which has no .group() for 返回None ,没有.group()

  moveHold = float(matches.group(1))

so that you get the said exception. 这样您就可以得到上述例外。

You should change that to 您应该将其更改为

    matches = re.search("Current_State==1 and Previous_State==0", output)
    if matches:
        moveHold = float(matches.group(1))
        resultm = client.service.retrieveMove(moveHold)
        ...
    else:
        # code for if it didn't match

At the point where you wrote 在你写的那一点

matches.group(...)

matches was None . 比赛None It seems that your regex search failed to find a match. 您的正则表达式搜索似乎找不到匹配项。 If it is possible for the regex search to fail, then you need to handle that scenario explicitly: 如果正则表达式搜索有可能失败,那么您需要显式处理该情况:

if matches is None:
    ....

Or perhaps the real problem is that your code to perform the search is just wrong. 也许真正的问题是执行搜索的代码是错误的。

Rather than me trying to tell you precisely what to do to fix the problem, the main point for you to learn is how to interpret this particular error message. 除了让我确切地告诉您解决问题的方法外,您要学习的重点还在于如何解释此特定错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM