gpt4 book ai didi

python - 分配变量并在所有值分配后发送到数据库python

转载 作者:行者123 更新时间:2023-11-29 22:51:56 24 4
gpt4 key购买 nike

这是我的第一个Python脚本。我正在尝试从 Arduino 获取数据,在 Raspberry Pi 上读取数据并将其保存到数据库中。代码单独工作(我可以正确分配变量并将数据发送到数据库,但似乎无法让它们都工作。我不确定我的逻辑是否有效(将变量设置为 null,然后在它们都具有后保存)值)。感谢您的输入。

    import re
import serial
import MySQLdb
import time

db =MySQLdb.connect(host = "localhost",user = "root",passwd = "example", db = "arduino")
ser =serial.Serial('/dev/ttyACM0',9600)

humidityPattern = "Humidity\:(\d\d\.\d\d)"
tempDhPattern = "TemperatureDH\:(\d\d\.\d\d)"
barometerPattern = "PressureBMP\:(\d\d\.\d\d)"
tempBmpPattern = "TemperatureBMP\:(\d\d\.\d\d)"
tempTmpPattern = "TemperatureTMP\:(\d\d\.\d\d)"
blLightPattern = "BLLight\:(\d+)"
brLightPattern = "BRLight\:(\d+)"
frLightPattern = "FRLight\:(\d+)"
flLightPattern = "FLLight\:(\d+)"


while 1:
line = ser.readline()
humidity = None
tempDh = None
pressure = None
tempBmp = None
tempTmp = None
blLight = None
brLight = None
frLight = None
flLight = None

#Humidity Sensor
m = re.match(humidityPattern, line)
if m is not None:
humidity = m.group(1)
print "Humidity is "+humidity

m = re.match(tempDhPattern, line)
if m is not None:
tempDh= m.group(1)
print "Humidity Temp is "+tempDh

#Pressure Sensor
m = re.match(barometerPattern, line)
if m is not None:
pressure = m.group(1)
print "Pressure is "+tempDh

m = re.match(tempBmpPattern, line)
if m is not None:
tempBmp= m.group(1)
print "Pressure Temp is "+tempBmp

#Temp Sensor
m = re.match(tempTmpPattern, line)
if m is not None:
tempTmp= m.group(1)
print "Temp is "+tempTmp

#Light Sensors
m = re.match(blLightPattern, line)
if m is not None:
blLight= m.group(1)
print "BL Light is "+ blLight

m = re.match(brLightPattern, line)
if m is not None:
brLight= m.group(1)
print "BR Light is "+ brLight

m = re.match(frLightPattern, line)
if m is not None:
frLight = m.group(1)
print "FR Light is "+ frLight

m = re.match(flLightPattern, line)
if m is not None:
flLight = m.group(1)
print "FL Light is "+ flLight

if humidity and tempDh and pressure and tempBmp and tempTmp and blLight and brLight and frLight and flLight is not None:
with db:
cur = db.cursor()
cur.execute('insert into weather(humidity, temp_dh, pressure,temp_bmp, temp_tmp, bl_light, br_light, fr_light, fl_light) values("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")'%(humidity, tempDh, pressure, tempBmp, tempTmp, blLight, brLight, frLight, flLight ))
time.sleep(5)
print 'upload'

最佳答案

测试中存在一个问题:

if humidity and tempDh and pressure and tempBmp and tempTmp and blLight and brLight and frLight and flLight is not None:

仅测试最后一个变量 flLightNone。然而,如果看起来所有其他都是 None 或非空字符串,那么这个应该会意外地工作,因为 None 是假的并且每个非空字符串都是真实的。

因此,一个更大的问题是,每次循环时,您都会丢弃以前读取过的所有值,无论您是否保存了它们。

要解决这个问题,请添加一个 bool 标志 must_init 并将逻辑更改为在循环开始时类似的内容:

must_init = True而真实: 行 = ser.readline() 如果必须_init: 湿度 = 无 温度Dh = 无 压力=无 tempBmp = 无 临时温度 = 无 blLight = 无 br光=无 frLight = 无 flLight = 无 必须初始化=假

并且仅在现在最终的print 'upload'之后的with语句的最后再次设置must_init = True

这样,您将仅在(A)第一次或(B)将之前的值保存到数据库后立即将所有变量清空,这似乎是更正确的逻辑。

其他简化改进也是可能的(例如,将变量保留为字典中的项目,这样您就不必在 any 检查中枚举它们;将 RE 表达式也放在由与您在变量字典中使用的名称相同,以极大地压缩和简化您的代码)但关键点是,按照我之前的建议添加 must_init bool 标志的代码应该工作 -- 之后你可以改进它!-)

关于python - 分配变量并在所有值分配后发送到数据库python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28869970/

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