Python,从元组中的整数中删除逗号

Python, remove comma from integer in tuple

如何从元组中的整数中删除逗号?

mycursor.execute("SELECT score FROM highscore WHERE userID=4")
highscore = mycursor.fetchall()

for x in highscore:
    print(x)

我正在从数据库中获取数字,这是我的输出

Output:
(324,)
(442,)
(100,)

谁能告诉我如何删除整数末尾的逗号? 如果有任何帮助,我将不胜感激:)

您正在打印元组。要访问该值,只需执行

for x in highscore:
    print(x[0])

for (x,) in highscore:
    print(x)

长话短说...你不能。

该逗号用于将元组与使用括号表示法声明的 int 区分开来,例如:

# This is an int
a = (
  2
)

# This is a tuple
b = (
  2,
)

如果您不需要逗号,请使用列表或集合,或者只使用一个整数。但是内部只有一个元素的元组将始终有尾随逗号。