5

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.

smci
  • 32,567
  • 20
  • 113
  • 146
curious_cosmo
  • 1,184
  • 1
  • 18
  • 36

1 Answers1

2

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'.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • So if I then update my table with these values, will it update correctly? As in the 75379L will be entered into a column with integer data type without an error? – curious_cosmo Sep 25 '17 at 20:11
  • Yes, `int` and `long` are interchangeable. BTW, if you print an individual value, you won't see the trailing 'L'. You're seeing it because you're printing a container. – Eugene Yarmash Sep 25 '17 at 20:14
  • I find the docs confusing in that. Can you really overflow a python int but not a long with some value? I thought that distinction was gone many years ago? – roganjosh Sep 25 '17 at 20:18
  • 1
    @roganjosh: No, Python automatically creates a long integer if a value is too large to be represented as a plain integer. MySQLdb just seems to do that explicitly. – Eugene Yarmash Sep 25 '17 at 20:23