Search code examples
pythonmysqlarraysmysql-pythonfetchall

Printing results from cursor.fetchall() without the trailing 'L'


I have a python code that accesses mysql through MySQLdb to run a select statement. I then use cursor.fetchall() to gather that output as an array, but the problem is it prints an L at the end of each output like so:

sql = "SELECT data1, data2  FROM table1, table2 ;"
cursor.execute(sql)
dataarray = cursor.fetchall()
print dataarray

>>> ((75379L, 45708L), ...)

But in my table, there are no L's, just the numbers. How can I ensure that it just fetches and prints the data without the L? I'd rather avoid using [:-1] on a string because then it changes my None values to Non.


Solution

  • The L indicates the long integer type. In Python 2 that's used automatically for values that are too large to be represented as a plain integer.

    MySQLdb appears to just always return longs:

    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.

    Calling str() on those values should omit the trailing 'L'.