我有以下内容 -
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
如果我只打印它们(而不是将它们添加到列表中),它会打印出正确的值。如何才能将此代码成功添加到此列表中?我尝试将它们更改为浮点数,但也出现了相同的错误。
感谢。
答案 0 :(得分:2)
您需要使用append
代替extend
。
见:
L.extend(iterable) - >无 - 通过附加可迭代
中的元素来扩展列表
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')]