Print only numbers from table from database in mysql

Owen Penn :

Sorry this question is so badly worded. Basically, I have a highscore column in my database, and I want to query the database for these values in descending order. This is how I so this:

highscore_list = ("SELECT highscore FROM users ORDER BY highscore DESC")
cursor.execute(highscore_list)
results = cursor.fetchall()
for i in results:
    print(i)

The values I have in the highscore column are 0, 0, 1000. When I run this code I get an output of (1000,) (0,) (0,) Is there way to remove the brackets and comma so I am instead left with 1000 0 0. I am using python and mysql.

AzyCrw4282 :

You can see here to find out why that is caused and how it can be fixed within your code - mysql remove brackets from printed data

You can also, since cursor.fetchall() returns an iterable list, you can simply modify the values in a pythonic manner.

For example,

lst = [(1000,) , (0,) , (0,)]
correct = [str(x)[1:-2] for x in lst ]
results = list(map(int, correct))
print(results)

outputs:

root@00973a947f4e:/root# python app.py
[1000, 0, 0]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related