Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有Python元组的Mysql SELECT IN子句_Python - Fatal编程技术网

带有Python元组的Mysql SELECT IN子句

带有Python元组的Mysql SELECT IN子句,python,Python,Python新手…我想从for循环创建一个新的元组。元组将在in子句中的MySQL select查询中使用。它工作不正常,告诉我在cur.execute行上有以下问题 TypeError:在字符串格式化过程中并非所有参数都已转换 check_tz = [] for tz in pytz.common_timezones: if now_utc.astimezone(timezone(tz)).strftime('%H') == '01': check_tz.append(

Python新手…我想从for循环创建一个新的元组。元组将在
in
子句中的MySQL select查询中使用。它工作不正常,告诉我在
cur.execute
行上有以下问题

TypeError:在字符串格式化过程中并非所有参数都已转换

check_tz = []
for tz in pytz.common_timezones:
    if now_utc.astimezone(timezone(tz)).strftime('%H') == '01':
        check_tz.append(tz)

print check_tz

# Prints out something as follows. I cut the output short for the sake of this example.
# ['America/Anguilla', 'America/Antigua', 'US/Eastern']

cur.execute("SELECT * FROM users where timezone IN (%s)", check_tz)

for row in cur.fetchall():
    print row[0]

您可能需要将时区列表转换为字符串:

cur.execute("SELECT * FROM users where timezone IN (%s)", ','.join(map(lambda x: "'%s'" % x, check_tz))

不起作用,链接到可能的重复项中的解决方案完全起作用。@幸运的是,它确实缺少了元素的引用。是的。很好用!