我的类型发生了什么,TypeError((Python)

2024-09-27 04:18:57 发布

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

我有一个从SQLite数据库检索数据的方法。数据保存为文本(出于某种原因),但实际上它是浮点数(14.5等)。这是我获取它们的方法:

def retrieveSpeeds(databasepath, someid):
     con = lite.connect(databasepath)
     with con:
        cur = con.execute("SELECT speed FROM speeds WHERE id = someid")
        speeds = [x[0] for x in cur]
        return speeds

这将返回以下内容:

[u'14.00', u'14.50', u'14.50', u'14.50', u'14.50', u'13.80']

但是,因为我想要普通数字,所以我需要:

for i in range(0, len(speed)):
     newspeeds.append(float(speed[i]))

因此,现在新的回报如下所示:

[14.0, 14.5, 14.5, 14.5, 14.5, 13.8]

因此,我主要是:

maxspeeds = []
for id in userid:
    speed = retrieveSpeeds(databasepath, id)
    if len(speed>0):
           maxspeeds.append(max(speed))
for i in range(0,len(maxspeeds)):
    if maxspeeds[i] > 40:
          maxspeeds = maxspeeds.pop(i)

这给了我以下类型的错误:

    Traceback (most recent call last):
    if len(speed>0):
    TypeError: object of type 'bool' has no len()

布尔?我非常困惑,为什么我返回的列表现在是一个bool


Tags: 数据path方法inidforlenif
2条回答

错误消息显示您编写了if len(speed>0):(请注意len中的>0),而不是您本应编写的len(speed)>0(以及您在代码段中所做的)

听着,你有一个打字错误,正确的方法是做if len(speed)>0:。你自己已经改正了

len(5>2)也不起作用,因为5>2返回True并且True没有len()

相关问题 更多 >

    热门问题