0
import sqlite3

conn = sqlite3.connect('contacten.db')

c = conn.cursor()

#c.execute("""CREATE TABLE mail (
#           mail text
#           )""")

#c.execute("INSERT INTO mail VALUES ('test@gmail.com')")

conn.commit()

c.execute("SELECT * FROM mail")

print(c.fetchall())

conn.commit()
conn.close()

This is the code I've made but as a result, I get:

[('test@gmail.com',), ('test1@gmail.com',), ('test2@gmail.com',)]

But in this array I have a comma too much. ,), like this. does anyone of you know how to get rid of this extra comma?

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59
  • c.fetchall() returns a list of lists... it's normal to be separated with commas – mlwn Mar 21 '20 at 12:37
  • 2
    The comma is just part of the representation of a single-element tuple, not part of the data itself. – chepner Mar 21 '20 at 12:44

3 Answers3

3

The commas are there for a good reason, your result is a list of tuples; this is a consequence of how sqlite represents the result set, the data itself doesn't contain commas:

result = c.fetchall()
print(result)
=> [('test@gmail.com',), ('test1@gmail.com',), ('test2@gmail.com',)]

That's because each row can have more than one field. In your case you only have one field, but Python can't just remove the comma, because if we did you'd end up with a list of elements between brackets, not a list of tuples (see here to understand why).

Of course, if you're certain that the result will only have one field per row, you can simply get rid of the tuples by extracting the one (and only) field from each row at the moment of printing the result:

result = c.fetchall()
print([f[0] for f in result])
=> ['test@gmail.com', 'test1@gmail.com', 'test2@gmail.com']
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

Using python zip:

>>> emails = [('test@gmail.com',), ('test1@gmail.com',), ('test2@gmail.com',)]
>>> emails, = zip(*emails)
>>> type(emails)
<type 'tuple'>
>>> emails
('test@gmail.com', 'test1@gmail.com', 'test2@gmail.com')
>>> list(emails)
['test@gmail.com', 'test1@gmail.com', 'test2@gmail.com']
andrei1111
  • 454
  • 4
  • 6
0
import sqlite3                                                                                      

conn = sqlite3.connect('contacten.db')                                                              

c = conn.cursor()                                                                                   

#c.execute("""CREATE TABLE mail (                                                                       
#           mail text                                                                               
#           )""")                                                                                   
#c.execute("INSERT INTO mail VALUES ('test@gmail.com')")                                            
#c.execute("INSERT INTO mail VALUES ('test1@gmail.com')")                                           
#c.execute("INSERT INTO mail VALUES ('test2@gmail.com')")                                           

conn.commit()                                                                                       

c.execute("SELECT * FROM mail")                                                                     

#print(c.fetchall())                                                                                

for item in c.fetchall():                                                                           
    print item[0]                                                                                   

conn.commit()                                                                                       
conn.close()                                                                                        
mlwn
  • 1,156
  • 1
  • 10
  • 25