1
@staticmethod
def next_group_id():
    cursor = connections['ips4'].cursor()
    cursor.execute(Ips4Manager.SQL_GET_ALL_GROUP_IDS)
    row = cursor.fetchall()
    if row is not None:
        string_num = str(row)
        max_number = max(string_num)
        print max_number
    else:
        logger.debug("cannot determine next group id")
        return False
outputs : (8L,)

Whats the most efficient way to convert it to a string so i only get 8?
I'm new at python any help would be appreciated.

user5601513
  • 33
  • 2
  • 6
  • To add more information the SQL query responds with ((2L,), (3L,), (4L,), (6L,), (8L,)) i pull the max number with max() and get 8L when i try to convert that to an int it gives me an error saying L is not base 10. – user5601513 Mar 31 '16 at 21:48
  • Have you tried things like str(int(row[0])) or str(row[0])? str((8L,)[0]) should produce 8. The responds look like tuples. – Haochen Wu Mar 31 '16 at 21:52
  • 1
    I don't understand how your code can be outputting `(8L,)`. The `max` of `string_num` can only ever be a single character (the "largest" character in the string). – Dunes Mar 31 '16 at 21:52

1 Answers1

2

What you are seeing is that the information in row is actually a list of tuples. Each of those tuples has one member.

Try printing max(row)[0].

See this example;

In [7]: row = [(1,), (2,), (8,)]

In [8]: max(row)
Out[8]: (8,)

In [9]: print(max(row)[0])
8

(You're not seeing L here because I'm using Python 3)

Just to show that it works in Python 2:

Python 2.7.11 (default, Jan  9 2016, 15:47:04) 
[GCC 4.2.1 Compatible FreeBSD Clang 3.4.1 (tags/RELEASE_34/dot1-final 208032)] on freebsd10
Type "help", "copyright", "credits" or "license" for more information.
>>> row = [(1L,), (2L,), (8L,)]
>>> max(row)
(8L,)
>>> print(max(row)[0])
8
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • You wouldn't see the L in python 2 either. The `str` of a `long` does not append an L, only `repr` does that (and `print` uses `str`). – Dunes Mar 31 '16 at 21:57