无法在列表中添加项:“无类型”对象没有属性“追加”

2024-10-03 15:31:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python 3.7中的以下代码从各种传感器获取数据,然后将它们远程存储在NAS上的MySQL数据库中

dataset = []
try:
    while count<max:
        humidite, temp = Adafruit_DHT.read_retry(sensor, pin)
        time.sleep(2)
        if humidite is not None and temp is not None:
            print("avant ", dataset)
            #print("La temperature est de : {0:0.1f}*C Humidité de : {1:0.1f}%".format(temp, humidite))
            dataset = dataset.append((time.ctime(),'lieu',temp,humidite))
            print("apres ", dataset)
            count+=1
        else:
            print("Lecture des informations impossible")
except KeyboardInterrupt:
    print("End of work")

dbase = get_connect()
add_data(dataset)
dbase.close()

我添加了一些打印以查看“数据集”。循环在第二次传递时失败(计数=1),第一次传递的数据集结果为:

avant []
apres None

.append方法是否有问题


Tags: 数据nonetimeiscountnotdetemp
1条回答
网友
1楼 · 发布于 2024-10-03 15:31:16

换行

dataset = dataset.append((time.ctime(),'lieu',temp,humidite))

dataset.append((time.ctime(),'lieu',temp,humidite))

方法list.append是一个就地操作,返回None,然后将其分配回列表

相关问题 更多 >