python mysqldb单行/列返回元组值

2024-09-30 02:23:38 发布

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

db_quote_cursor.execute("""select lastince 
                           from `here` 
                           order by `date` desc, `time` desc 
                           limit 1""")
thisout= db_quote_cursor.fetchone()

thisout是一个浮点值。我需要对它进行计算,但不能将其转换为元组中的浮点。在

!work(pleasework=float(thisout[0]))

我试着把它转换成一个列表,但也没用。当我不想/不需要循环访问元组中的值时,如何直接访问它?或者别的什么。。。在

以下是我从数据库中得到的信息:

^{pr2}$

这是我得到的错误:

>>> result = float(thisout[0]) 
Traceback (most recent call last): File "file.1.py", line 56, in <module> 
TypeError: float() argument must be a string or a number 

Tags: fromexecutedbbyhereorderfloatselect
2条回答

既然你发布了回溯,情况就更清楚了。谢谢您。在

您正在取回一个^{}对象。
事实上,这和浮球一样好。在

您不必转换它,只需开始计算:

thisout[0] += 1 # works fine

但是,请注意,您不能:

^{pr2}$

在上述情况下,您可以改为:

import decimal as dc
thisout[0] += dc.Decimal('1.0')

如果结果只是一个1元组,那么您可以使用thisout[0]按照bernie的说法访问它(您的代码显然在尝试)。我猜您在尝试解析float时会有不同的错误;发布stacktrace或异常。在

相关问题 更多 >

    热门问题