0

Python newbie here...I want to create a new tuple from a for loop. The tuple will be used inside a MySQL select query in the IN clause. It's not working correct, telling me I have the following issue on the cur.execute line.

TypeError: not all arguments converted during string formatting

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]
1
  • Yep. works perfectly! Commented Jul 12, 2013 at 5:43

1 Answer 1

1

You probably need to convert the list of timezones to string:

cur.execute("SELECT * FROM users where timezone IN (%s)", ','.join(map(lambda x: "'%s'" % x, check_tz))
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work, the solution in the link to the possible duplicate works perfectly.
@luckytaxi Indeed, it was missing quoting of elements.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.