简体   繁体   English

如何摆脱逗号?

[英]How to get rid of a comma?

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?你们中有人知道如何摆脱这个多余的逗号吗?

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:这是sqlite如何表示结果集的结果,数据本身不包含逗号:

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).在您的情况下,您只有一个字段,但 Python 不能只删除逗号,因为如果我们这样做,您最终会得到括号之间的元素列表,而不是元组列表(请参阅此处了解原因)。

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']

Using python zip :使用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']
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()                                                                                        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM