gpt4 book ai didi

python - 属性错误 : 'NoneType' object has no attribute 'group'

转载 作者:行者123 更新时间:2023-11-28 22:50:24 32 4
gpt4 key购买 nike

请帮忙,因为我正在尝试使用 raspberry pi pir 传感器将 pir 传感器收集的数据(即 1 或 0)传输到网络服务我得到了这个错误

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() for

  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

关于python - 属性错误 : 'NoneType' object has no attribute 'group' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22681763/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com