我有以下 -
db = mariadb.connect(host=host,
user=someuser,
passwd=somepass)
cursor = db.cursor(mariadb.cursors.DictCursor)
#Get the top 5 largest tables inside of the database
cursor.execute('SELECT table_schema as `Database`, table_name AS `Table`, round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` FROM information_schema.TABLES ORDER BY (data_length + index_length) DESC LIMIT 5;')
data = cursor.fetchall()
for item in data:
dbsize.extend(float(item['Size in MB']))
这给了我以下错误 -
'Decimal' object is not iterable
如果我只是打印它们(而不是将它们添加到列表中),它会打印出正确的值。如何获得此代码以成功将每个项目添加到此列表中?我尝试将它们更改为浮点数,但得到了同样的错误。
谢谢。
您需要使用append
而不是extend
. 看:
L.extend(iterable) -> None -- 通过添加可迭代元素来扩展列表
a = []
In [131]: from decimal import Decimal
In [132]: f = Decimal(23.12)
In [133]: f
Out[133]: Decimal('23.120000000000000994759830064140260219573974609375')
In [134]: a.extend(f)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-134-13e0f548b372> in <module>()
----> 1 a.extend(f)
TypeError: 'decimal.Decimal' object is not iterable
In [135]: a.append(f)
In [136]: a
Out[136]: [Decimal('23.120000000000000994759830064140260219573974609375')]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句