0

I have this array:

rows = ['1393586700', 'BLAHBLAH', 'BLEHBLEH', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']

and this format String:

format String:   """%s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s"""

and this fails:

print 'test: ', formatStr % rows

    print 'test: ', formatStr % rows

TypeError: not enough arguments for format string

Why is it failing? there are exactly the same number of %s and fields!

Thanks!

3 Answers 3

3

This happens because you are printing a list instead of the expected tuple.

Observe this,

>>> print """%s %s""" % [1, 2]
TypeError: not enough arguments for format string

vs.,

>>> print """%s %s""" % (1, 2)
1 2

converting list to a tuple can be done with the tuple() function:

>>> print """%s %s""" % tuple([1, 2])
1 2
Sign up to request clarification or add additional context in comments.

5 Comments

Hello, Ok, that's clear. Now I'm trying to insert those into mysql and get the same thing, "TypeError: not enough arguments for format string". I see the documentation says it should be an array, and that's what I'm passing.. "rows" is an array... BUT it has only one record, not multiple records...
rows is a list, that's not the same as an array
@DavidVillasmil Python has a special-purpose "C"-like array data type, but one almost always uses lists, and one typically means a list even if talks about an array in python. Are you sure the documentation was talking about an array?
@mockinterface, not sure, not really... looking at it more carefully it looks like a list of tuples? data = [ ('Jane', date(2005, 2, 12)), ('Joe', date(2006, 5, 23)), ('John', date(2010, 10, 3)), ] stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)" cursor.executemany(stmt, data)
Ok, i finally got it. The idea was to create a list of records (tuples) but as I only had 1 record I ended up with just a list. now, i changed the way i create the array so that for each record I add a tuple to the array, and that works fine for mysql's insert, but of course not for the format string as that's only for 1 record. anyway, thank you for helping me understand!!
1

You should pass the tuple instead of the list

>>> rows = ['1393586700', 'BLAHBLAH', 'BLEHBLEH', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']
>>> f = """%s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s ,  %s"""
>>>
>>> f % tuple(rows) 

'1393586700 ,  BLAHBLAH ,  BLEHBLEH ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  1 ,  1 ,  0 ,  0'
    >>>

1 Comment

All right, this worked out perfectly. I wonder why, I am using the same exact procedure to insert in mysql using execute many with multiple rows and that works just fine... Anyway, Thanks!
0

Use str.join

>>> rows = ['1393586700', 'BLAHBLAH', 'BLEHBLEH', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '1', '1', '0', '0']
>>> 
>>> ", ".join(rows)
'1393586700, BLAHBLAH, BLEHBLEH, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.