我有一个 python 代码,它通过 MySQLdb 访问 mysql 以运行 select 语句。然后我使用 cursor.fetchall()
将该输出收集为一个数组,但问题是它在每个输出的末尾打印一个 L
,如下所示:
sql = "SELECT data1, data2 FROM table1, table2 ;"
cursor.execute(sql)
dataarray = cursor.fetchall()
print dataarray
>>> ((75379L, 45708L), ...)
但在我的表格中,没有 L,只有数字。我怎样才能确保它只在没有 L 的情况下获取和打印数据?我宁愿避免在字符串上使用 [:-1]
,因为它会将我的 None
值更改为 Non
。
最佳答案
L
表示 long
整数类型。在 Python 2 中,它会自动用于太大而无法表示为普通整数的值。
MySQLdb
似乎总是返回 long
s:
While MySQL's INTEGER column translates perfectly into a Python integer, UNSIGNED INTEGER could overflow, so these values are converted to Python long integers instead.
对这些值调用 str()
应该省略尾随 'L'
。
关于python - cursor.fetchall() 的打印结果没有尾随 'L',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46413320/