无法在列表中添加项目:'none type' 对象没有属性 'append'

failed to add item in a list : 'none type' object has no attribute 'append'

我尝试在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()

我加了一些打印出来看看"dataset"。循环在第二次通过时失败(计数 = 1),第一次通过的数据集结果为:

avant []
apres None

.append方法有问题吗?

更改此行

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

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

方法 list.append 是就地操作,returns None 然后您将其分配回您的列表。