2014-03-27 59 views
1

請大家幫忙,因爲我正在嘗試使用覆盆子pi pir傳感器將pir傳感器(它是1或0)收集的數據傳輸到web服務 以及我得到這個錯誤AttributeError:'NoneType'對象沒有屬性'group'

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' 

,這是我的代碼

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) 

回答

1

,在您寫

matches.group(...) 

比賽是None。看來你的正則表達式搜索沒有找到匹配。如果有可能的正則表達式搜索失敗,那麼你需要明確處理這個場景:

if matches is None: 
    .... 

也許真正的問題是,你的代碼執行搜索是錯誤的。

與其試圖精確地告訴你如何解決問題,學習的重點是如何解釋這個特定的錯誤信息。

2

然後顯然output不包含預期的字符串。 (究竟應該如何,當它通過調用echo 18產生的?)因此,

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

回報None,它沒有.group()

moveHold = float(matches.group(1)) 

,讓你得到上述例外。

你應該改變,要

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