gpt4 book ai didi

python - 将长整型转换为字符串 Python

转载 作者:行者123 更新时间:2023-12-01 04:01:51 27 4
gpt4 key购买 nike

@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,)

将其转换为字符串的最有效方法是什么,这样我只能得到 8?
我是 python 新手,任何帮助将不胜感激。

最佳答案

您所看到的是,row 中的信息实际上是一个元组列表。每个元组都有一个成员。

尝试打印 max(row)[0]

参见此示例;

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

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

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

(这里看不到 L,因为我使用的是 Python 3)

只是为了证明它可以在 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

关于python - 将长整型转换为字符串 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36344178/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com