【问题标题】:failed to add item in a list : 'none type' object has no attribute 'append' [duplicate]未能在列表中添加项目:“无类型”对象没有属性“附加”[重复]
【发布时间】:2020-04-30 20:59:01
【问题描述】:

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

我添加了一些打印来查看“数据集”。循环在第二遍失败(count=1),第一遍的数据集结果是:

avant []
apres None

.append 方法有问题吗?

【问题讨论】:

  • append 修改现有列表并返回None。

标签: python list append attributeerror nonetype


【解决方案1】:

改变这一行

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

到

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 2017-03-26
    • 2021-04-15
    • 2018-08-20
    • 1970-01-01
    • 2019-07-26
    • 2015-12-03
    相关资源
    最近更新 更多