2017-12-08 10 views
1

SQLクエリを保存するために設定ファイル(タイプ.ini)を使用すると、そのキーでクエリが取得されます。私はエラーを取得するPython - 設定ファイルの値に%sを使用

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 

TypeError: not all arguments converted during string formatting

だからで文字列をフォーマットしようと、私が使用し

;the ini file 

product_by_cat = select * from products where cat =%s 

:すべては、パラメータを使用してクエリを作成するまで、例を正常に動作しますiniファイルから値を取得します。 私の問題の提案。

答えて

0

あなたはこの

iniファイル

product_by_cat = select * from products where cat ={} 

Pythonのようなformat機能を使用することができます。ConfigParserのバージョン(Pythonの2またはPythonの3)に応じて、

complete_query= query.format(args) 
0

あなたがする必要があるかもしれません%をダブルクリックするか、エラーがスローされます:

product_by_cat = select * from products where cat =%%s 

良い方法は%チャーは解釈されないように、設定パーサーの生のバージョンを使用することであろうが

config = configparser.RawConfigParser() 
関連する問題