谁能告诉我为什么我得到的输出不正确。
query=("select Id from project where ActiveFlg='Y' ")
cursor.execute(query)
active_project=cursor.fetchall()
print(active_project)
输出:
((9L,), (10L,))
((5L,), (3L,))
预期输出:
(9,10)
(5,3)
最佳答案
你本身并没有错。表的数据类型是long
,因此会附加L
。 cursor.fetchall()
方法以 tuple
形式返回输出。
但是,要按照您想要的方式进行操作,请尝试:
In [0]: output = ((9L,),(10L,))
In [1]: x = tuple(sum(output, ()))
In [2]: x
Out[2]: (9L, 10L)
In [3]: y = [int(i) for i in x]
In [4]: y
Out[4]: [9, 10]
In [5]: tuple(y)
Out[5]: (9, 10)
简洁,使用:
In [6]: tuple([(int(i[0])) for i in output])
Out[6]: (9, 10)
cursor.fetchall()
返回一组行。因此,对于您的情况,请执行以下操作:
for row in active_project:
x = tuple([(int(i[0])) for i in row])
print x
关于python - 在python中使用的mysql查询中获取输出(某个整数L),例如(9L),其中包含9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45386562/