user5601513
user5601513

Reputation: 33

Convert Long to String Python

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

Upvotes: 1

Views: 7276

Answers (1)

Roland Smith
Roland Smith

Reputation: 43495

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

Upvotes: 2

Related Questions