![](/img/trans.png)
[英]AttributeError: 'NoneType' object has no attribute 'group' ,
[英]AttributeError: 'NoneType' object has no attribute 'group'
请帮忙,因为我尝试使用树莓派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'
这是我的代码
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)
那么显然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
在你写的那一点
matches.group(...)
比赛None
。 您的正则表达式搜索似乎找不到匹配项。 如果正则表达式搜索有可能失败,那么您需要显式处理该情况:
if matches is None:
....
也许真正的问题是执行搜索的代码是错误的。
除了让我确切地告诉您解决问题的方法外,您要学习的重点还在于如何解释此特定错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.