164

Here's the output. These are utf-8 strings I believe... some of these can be NoneType but it fails immediately, before ones like that...

instr = "'%s', '%s', '%d', '%s', '%s', '%s', '%s'" % softname, procversion, int(percent), exe, description, company, procurl

TypeError: not enough arguments for format string

Its 7 for 7 though?

5 Answers 5

291

You need to put the format arguments into a tuple (add parentheses):

instr = "'%s', '%s', '%d', '%s', '%s', '%s', '%s'" % (softname, procversion, int(percent), exe, description, company, procurl)

What you currently have is equivalent to the following:

intstr = ("'%s', '%s', '%d', '%s', '%s', '%s', '%s'" % softname), procversion, int(percent), exe, description, company, procurl

Example:

>>> "%s %s" % 'hello', 'world'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> "%s %s" % ('hello', 'world')
'hello world'
Sign up to request clarification or add additional context in comments.

2 Comments

That worked..sort of. I was trying to use %s formatting to avoid the NoneType error, but I'm getting it still. Related: stackoverflow.com/questions/1338690/…
Won't be useful to the OP, given the age, but just in case still useful to someone: a simple workaround where a variable may be none is to guard against that with a ternary statement - i.e. "%s %s" % (a if a is not None else "", b) if we are worried about a possibly being None. So even if a is None we still aren't passing None to the format operator and causing problems.
191

Note that the % syntax for formatting strings is becoming outdated. If your version of Python supports it, you should write:

instr = "'{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}'".format(softname, procversion, int(percent), exe, description, company, procurl)

This also fixes the error that you happened to have.

8 Comments

Does this work for the logging module as well? Is this kind of interpolation understood by it?
@joshsvoss: yes, there are a few links mentioned here that discuss the possibilities: bugs.python.org/issue14031
@SimeonVisser, why is this becoming outdated? If you, for example, use one variable to store format string and you want to put string with replaced values in same variable you get just a tad bit cleaner code with format_string %= ('bla', 'bla', 'bla'). Care to elaborate or point to some useful link about this?
Also, format() supports more explicit ways of expressing what happens, such as "{a} {b}".format(a='3', b='4').
I'm using python 3.6.5 and the percent sign operator still can be used...
|
29

I got the same error when using % as a percent character in my format string. The solution to this is to double up the %%.

3 Comments

Providing some code examples of how your solution works, and how it fails without it, etc. might be useful. See How do I write a good answer?. Note that this is a 5 year old question, so you should add an answer only if it provides significantly more information than those already there.
I got a variation of this error due to typo: "foo: %(foo)s, bar: s(bar)% baz: %(baz)s" % {"foo": "FOO", "bar": "BAR", "baz": "BAZ"}
For python3 this "solution" is wrong -> Invalid syntax
0

I had the same issue I was using a raw query for a specific reason and this was to add double quotes in TIME_FORMAT function.

User.objects.raw(
            f'SELECT f1,f2,TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(end_time) - TIME_TO_SEC(start_time))),"%%H:%%i") AS log FROM users GROUP BY start_dt')

Comments

0
def process_item(self, item, spider):
    sql = "insert into fd(province,pici,subject,maxscore,midscore,minscore,zy,school) values('%s', '%s','%s'," \
          "'%s','%s','%s','%s','%s','%s')"
    data = (item['province'],item['pici'],item['subject'],item['maxscore'],item['midscore'],item['minscore'],item['zy'],item['school'])

    self.cursor.execute(sql % data)

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.