Python error not enough arguments for format string - python

Can anyone tell me whats wrong with this:
put(('%s%s.tar.gz' % config.SERVER_PROJECT_PATH, config.RELEASE))
TypeError: not enough arguments for format string
I just want insert two variables in to the string, is my syntax correct?

You need to put the two values in a tuple:
put('%s%s.tar.gz' % (config.SERVER_PROJECT_PATH, config.RELEASE))
otherwise Python sees this as two separate expressions to form a tuple, '%s%s.tar.gz' % config.SERVER_PROJECT_PATH and config.RELEASE.

The syntax is incorrect. The string formatting arguments must be a tuple. You are creating a tuple with the formatted string and the second formatting argument. Use this instead:
put("%s%s.tar.gz" % (config.SERVER_PROJECT_PATH, config.RELEASE))

Related

Why is this SQL INSERT INTO statement throwing an error: TypeError: not all arguments converted during string formatting

So my code looks like:
sqlpoptable = "INSERT INTO datas(createdon) VALUES (%s);"
data = ('a')
cursor.execute(sqlpoptable, data)
postgresConnection.commit()
Which works fine. However if I try to add any length to the variable such as:
sqlpoptable = "INSERT INTO datas(createdon) VALUES (%s);"
data = ('aa')
cursor.execute(sqlpoptable, data)
postgresConnection.commit()
I get the error:
TypeError: not all arguments converted during string formatting
What is going on that is making using any more than one character throw an error? Ultimately what I am trying to do is timestamp on entry for this column.
data type is character varying 256.
The second argument to cursor.execute() must be a sequence, such as a list or a tuple.
It looks like you tried to make a tuple here:
data = ('aa')
However, that is not a tuple; it is just a plain string. The syntax for one-element tuples is a bit funny:
data = ('aa',)
Your first example worked because strings are also considered to be sequences (of individual characters), and you used a one-character string, which matches the number of %s tokens in the sql statement.
Your second example did not work because you used a two-character string, so each character was interpreted as its own separate item, which was too many values for the single %s token.
Your First Statement Is Executed Because Your Are Putting correct character that is 'a' but in second case you are not putting correct character that is 'aa' thats why you got error.
character hold only length of one not more than one.

Python: not all arguments converted during string format

I have a panel for 5 years. Each person [aa_cod_fiscm] declares his income [cc_red_lrd] each year. I am trying to have a difference of declaration between each year and the previous one [difprev]. My code is
data["difprev"]= data.groupby(data.aa_cod_fiscm % 5).cc_red_lrd.diff()
All the variables are integers, but I get the following error
TypeError: not all arguments converted during string formatting
I don't know why. Can you help me , please?
I think your issue is that the % is being evaluated as a formatter rather than a mod function because data.aa_cod_fiscm is evaluating as a str object. Maybe this is how it is stored in that attribute? You could try casting it to an int.
try this:
data["difprev"]= data.groupby(int(data.aa_cod_fiscm) % 5).cc_red_lrd.diff()

How to parse string with comma in Python?

I am having problems parsing a string to a function in python.
def logAppend(self, data):
print(data)
When I parse a string with a comma in the above code it returns the following.
TypeError: logAppend() takes exactly 2 arguments (3 given)
I am kinda new to Python, so please take it easy on me if I'm missing something simple here..
It's unclear what your string is, but if you're assuming it's (self, data), those two things are actually the variables for your function. Either could be calling a string, or more likely a list of strings, that are would be ['string1', 'string2'], for example.

Why does Python string formatting silently ignore mismatched argument counts with class instances?

Normally, Python old-style string formatting complains if the number of placeholders in the string doesn't match the number of arguments passed:
>>> 'no.placeholders.here' % 'test'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
However, when the argument passed is an instance of a user-defined class, it instead silently ignores it:
>>> class Test(object): pass
>>> 'no.placeholders.here' % Test()
'no.placeholders.here'
This behavior seems inconsistent and has resulted in some difficult-to-track-down bugs. Why does the type of the format argument matter for the purposes of this error?
This is exactly why %-formatting is old
%-formatting is well known for its inconsistencies about argument handling which result in exceptions or not depending on the type. There are only two ways to use % formatting and avoid inconsistencies:
Make sure the formatting string contains exactly one formatting field and pass the object to format as only right argument
Use a tuple or dict as right argument. Don't use lists, sets or Models. Only tuples and dicts.
These are taken from the documentation
If format requires a single argument, values may be a single non-tuple
object. Otherwise, values must be a tuple with exactly the number
of items specified by the format string, or a single mapping object
(for example, a dictionary).
Your examples do not fall in these two cases because you have 0 formatting fields, which is different than 1 and thus the right argument must be a tuple or a mapping, but you are passing a string and a user defined object. As such you are under "undefined behaviour".
The inconsistencies on the error messages were already discussed in this question (in my answer).
If you want more consistent behaviour, use str.format.

python Variable in a string

I am trying to specify a standard binary format length from a variable but for some reason it never works. Am I doing the formatting wrong or the variable inclusion?
comp.write("{0:%ib}".format(I) % num_bits)
ValueError: Invalid conversion specification
Firstly, it's in the wrong order:
("{0:%ib}" % num_bits).format(I)
Secondly, this isn't the way to do it! Mixing up types of formatting operator implies you don't know it can be done together. You want:
"{:{}b}".format(I, num_bits)
and if you really want to do it in two steps:
"{{:{}b}}".format(num_bits).format(I)
The {{ and }} are escaped, so are transformed to single braces, after the first .format.
You're doing the interpolation the wrong way round. You'll need to resolve the %i before passing it to format. This would work:
comp.write(("{0:%ib}" % num_bits).format(I))
but is pretty horrible, you probably want to split it into two:
fmt = "{0:%ib}" % num_bits
comp.write(fmt.format(I))

Categories