samara samara - 6 months ago 81
Ini Question

Python - Use %s in value of config file

I use a config file (type .ini) to save my SQL queries, then i get a query by its key. All work fine, until creating a query with parameters, example :

;the ini file

product_by_cat = select * from products where cat =%s


I use :

config = configparser.ConfigParser()
args= ('cat1')
config.read(path_to_ini_file)
query= config.get(section_where_are_stored_thequeries,key_of_the_query)
complete_query= query%args


I get the error :


TypeError: not all arguments converted during string formatting


So it try to format the string at retrieving the value from the ini file.
Any proposition of my problem.

Answer Source

You can use format function like this

ini file

  product_by_cat = select * from products where cat ={}

python:

 complete_query= query.format(args)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download