How to remove extra comma in cur.fetchall()

Question:

I tried to output all values in a column in mysql however, it also outputs extra comma in the end.

def numbers():
    db = getDB();
    cur = db.cursor()
    sql = "SELECT mobile_number FROM names"
    cur.execute(sql)
    result = cur.fetchall()

    for x in result:
        print(x)

It looks like this in shell:

(0123456789,)
(9876543210,)
Asked By: Paopao

||

Answers:

a couple more options:

# validate that the results contain exactly one column
for [x] in result:
    print(x)

and

# using argument unpacking
for x in result:
    print(*x)
Answered By: Sam Mason

Basically as Ben’s comment says the comma doesn’t technically exist, it is there because it is a tuple.

but is you still want to remove it try this:

rows=[i[0] for i in rows]
Answered By: Iggy Press
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.