Python Typeerror: Not Enough Arguments for Format String

Python TypeError: not enough arguments for format string

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.

Any suggestions when it shows TypeError: not enough arguments for format string in Python?

To directly answer the question, you need to change %[M]s to %(M)s in your code, like so:

kernel_code_template = """
__global__ void MatrixMulKernel(float *a,float *b,float *c){
int tx = threadIdx.x;
int ty = threadIdx.y;
float Pvalue = 0;
for(int i=0; i<%(N)s; ++i){
float Aelement = a[ty * %(N)s + i];
float Belement = b[i * %(M)s + tx];
Pvalue += Aelement * Belement;
}
c[ty * %(M)s + tx] = Pvalue;
}
"""

Then it will work as expected. However, I'd strongly recommend you start using f-strings as you indicated you were working with Python 3.7:

kernel_code_template = f"""
__global__ void MatrixMulKernel(float *a,float *b,float *c){{
int tx = threadIdx.x;
int ty = threadIdx.y;
float Pvalue = 0;
for(int i=0; i<{N}; ++i){{
float Aelement = a[ty * {N} + i];
float Belement = b[i * {M} + tx];
Pvalue += Aelement * Belement;
}}
c[ty * {M} + tx] = Pvalue;
}}
"""

I get there are trade-offs, though, namely those {{ and }} escapes. There is also the .format() method as @Brandt pointed out. Choose what feels best to you.

TypeError: not enough arguments for format string in python

In your queryset you have more than one %s although you pass %(user_id) one time

you should pass all parameter in format string you defined

cursor.execute("..." % (param1, param2, param3, param4,...))

Python DataFrame to MYSQL: TypeError: not enough arguments for format string

I'm no data scientist so there's probably a more elegant way to fix it directly with pandas. But the way I usually work with MySQL (and really any SQL drivers) is to give it lists of python tuples.

If you parse each row of the pandas data frame with for row in df.itertuples(): and craft each tuple carefully - making sure the types match the SQL table, all should work ;)

Example:

def insert_daily_data_into_db(data_vendor_id, symbol_id, daily_data):
'''
Takes a list of tuples of daily data and adds it to the MySQL database.
Appends the vendor ID and symbol ID to the data.

daily_data: List of tuples of the OHLC data (with adj_close and volume)
'''

# Create the time now
now = datetime.datetime.utcnow()

df = pd.DataFrame(data=daily_data[0])
daily_data = []
created_date = now
last_updated_date = now
for row in df.itertuples():
_index = row[0] # discard
date = row[1]
open = row[2]
high = row[3]
low = row[4]
close = row[5]
adj_close_price = row[6]
volume = row[7]
daily_data.append((int(data_vendor_id), symbol_id, date, created_date, last_updated_date, open, high, low, close, volume, adj_close_price))

# Connect to the MySQL instance
con = mdb.connect(host="localhost", user="user", password="yourpassword",
db="yourdbname", port=3306)

final_str = """
INSERT INTO daily_price (data_vendor_id, symbol_id, price_date, created_date,
last_updated_date, open_price, high_price, low_price, close_price, volume, adj_close_price)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""

with con:
cur = con.cursor()
cur.executemany(final_str, daily_data)
con.commit()

I've tried not to tamper with your existing code too much. Just enough to make it work.

I think what was happening there for you was that you're technically passing it a list of pandas dataframes with only a single pandas dataframe in the list. Instead what you want is a list of tuples with 11 fields to unpack per tuple.

Maybe you mean to pass the dataframe directly i.e. not contained inside of a list but I still don't think that would be right because 1) there's an "Index" column in the dataframe which would give erroneous results 2) you'd need to call some methods on the dataframe to retrieve only the values (not the headers to the columns) and transform it to the correct list of tuples. It's probably very doable but I will leave that to you to find out.

I am also assuming your table schema is something like this:

CREATE TABLE IF NOT EXISTS daily_price (
data_vendor_id INT,
symbol_id INT,
price_date DATETIME,
created_date DATETIME,
last_updated_date TIMESTAMP,
open_price VARCHAR(256),
high_price VARCHAR(256),
low_price VARCHAR(256),
close_price VARCHAR(256),
volume INT,
adj_close_price VARCHAR(256)
);

TypeError: not enough arguments for format string - Python Formatting Issue

My guess, based on the error message, is that the python interpreter thinks this format string should receive more arguments, because to the %G looks like a placeholder.

To fix it: replace each %Gwith %%G, the %% will be interpreted as a literal representation of the percent sign and not a placeholder prefix.

FYI: to replace an int you would usually use %d ( and %f for floating point values and for all the details -> string formating 101)

TypeError: not enough arguments for format string when using %s

You need to put your arguments for string formatting in parenthesis:

print (... % (name, last_name, gender, age))

Otherwise, Python will only see name as an argument for string formatting and the rest as arguments for the print function.


Note however that using % for string formatting operations is frowned upon these days. The modern approach is to use str.format:

print ("So your name is {}, your last name is {}, you are {} and you are {} years old".format(name, last_name, gender, age))


Related Topics



Leave a reply



Submit