在Python3中从fetchone()调用中检索值

在Python3中从fetchone()调用中检索值,python,sqlite,Python,Sqlite,我对python相当陌生,我在任何地方都找不到我的问题的答案。除非我不明白给出的答案。 我有一个数据库游标,我执行以下命令: cmd = 'select value from measurement where measurement_location is ?' crs.execute(cmd, [location_id]) print(crs.fetchone()) 其中打印: {'value': 73.97486139568466} 我需要用浮点数73.97。。。。在某些计算中,要计算

我对python相当陌生,我在任何地方都找不到我的问题的答案。除非我不明白给出的答案。 我有一个数据库游标,我执行以下命令:

cmd = 'select value from measurement where measurement_location is ?'
crs.execute(cmd, [location_id])
print(crs.fetchone())
其中打印:

{'value': 73.97486139568466}
我需要用浮点数73.97。。。。在某些计算中,要计算平均值。
我的问题是我不知道如何从fetchone()返回中提取浮点

fetchone
为每一行返回一个字典,将结果中的字段名(在本例中仅为
value
)与该行的值进行映射。使用它,你可以做到

cmd = 'select value from measurement where measurement_location is ?'
crs.execute(cmd, [location_id])
row = crs.fetchone()
print(row['value'])

print(row['value'] * 100)

或者,您可以对该结果执行任何其他操作

您可以执行以下操作:

row=crs.fetchone()

for x in row:

    print(x)

Value=x

您可以在计算中使用value变量

您也可以使用
行。get('value')
首先,这是不正确的。这是一本字典。当您使用for循环迭代字典时,您迭代它的键。OP想要得到这个值。其次,这是一种从字典或数组中获取元素的糟糕方法。x将绑定到iterable的最后一个元素,但它需要O(n)时间而不是O(1)。