简体   繁体   English

如何将元组列表转换为元组

[英]how to convert list of tuples to tuples

I have list like: 我有类似的清单:

L = [('a',1),('b',2),....]

I want to pass this tuples to the query for bulk insertion. 我想将此元组传递给查询以进行批量插入。

sql = "INSERT INTO [plma].["+tablename+"] VALUES {}".format(L)

but this qry takes L as [(),()] , where to execute this qry properly it needs to be only (),() 但是此qry将L用作[(),()] ,在哪里正确执行此qry只需为(),()

so I want only tuples from that list L, How to pass them ? 所以我只想要列表L中的元组,如何传递它们?

串化和剥离:

sql = "INSERT INTO [plma].["+tablename+"] VALUES " + str(L).strip('[]')

To join the elements of a list to a string, I would use .join() and list comprehension, as I find it rather nice and pythonic: 要将列表中的元素连接到字符串,我会使用.join()和列表理解功能,因为我发现它相当不错且具有Python风格:

values = ', '.join(str(e) for e in L)
sql = "INSERT INTO [plma].[{}] VALUES {}".format(tablename, values)

However, as has been pointed out, there may be better ways to approach the actual issue of inserting into a database. 但是,正如已经指出的那样,可能存在更好的方法来解决实际插入数据库的问题。

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

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