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.

3
  • 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. Commented Mar 31, 2016 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. Commented Mar 31, 2016 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). Commented Mar 31, 2016 at 21:52

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.