使用格式化输出自动扩展Python列表
Does anyone know if there's a way to automatically expand a list in Python, separated by commas? I'm writing some Python code that uses the MySQLdb library, and I'm trying to dynamically update a list of rows in a MySQL database with certain key values.
有没有人知道是否有一种方法可以自动扩展Python中的列表,用逗号分隔?我正在编写一些使用MySQLdb库的Python代码,我正在尝试使用某些键值动态更新MySQL数据库中的行列表。
For instance, in the code below, I'd like to have the numeric values in the record_ids list expand into a SQL "IN
" clause.
例如,在下面的代码中,我想让record_ids列表中的数值扩展为SQL“IN”子句。
import MySQLdb
record_ids = [ 23, 43, 71, 102, 121, 241 ]
mysql = MySQLdb.connect(user="username", passwd="secret", db="apps")
mysql_cursor = mysql.cursor()
sqlStmt="UPDATE apps.sometable SET lastmod=SYSDATE() where rec_id in ( %s )"
mysql_cursor.execute( sqlStmt, record_ids )
mysql.commit()
Any help would be appreciated!
任何帮助,将不胜感激!
5 个解决方案
#1
18
try:
",".join( map(str, record_ids) )
",".join( list_of_strings )
joins a list of string by separating them with commas
“,”。join(list_of_strings)通过用逗号分隔它们来连接字符串列表
if you have a list of numbers, map( str, list )
will convert it to a list of strings
如果您有一个数字列表,map(str,list)会将其转换为字符串列表
更多相关文章
- 使用PHP和Javascript的组合填充下拉列表
- mysql 排序两个字段/列表先根据时间升序排序,时间相同再根据商家I
- 从python列表创建MySql表
- [实战]MVC5+EF6+MySql企业网盘实战(27)——应用列表
- mysql中逗号分隔字段的更好替代方案
- 获取项目列表的更好方法:缓存序列化数据与数据库查询或其他?
- 如何从php中的数据库表创建表单下拉列表?
- MySQL查询返回用户的收件人列表
- 迭代angularjs中对象中的属性列表