Python TypeError:格式字符串的参数不足。我看不出有什么不对劲

Python TypeError:格式字符串的参数不足。我看不出有什么不对劲,python,Python,我的脚本中有这个python代码 dataBase = str(request.forms.get('db')) data = "/usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root %s -e \"show tables like \'f_\%s\'\"" %(dataBase) 但当我运行它时,它会给我以下错误:- TypeError: not enough

我的脚本中有这个python代码

dataBase = str(request.forms.get('db')) 
data =  "/usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf  -u root %s -e \"show tables like \'f_\%s\'\"" %(dataBase)
但当我运行它时,它会给我以下错误:-

TypeError: not enough arguments for format string
有什么帮助吗?

您的格式字符串中有两个
%s
标记:

... -u root %s -e \"show tables like \'f_\%s\' ...
            ^^                            ^^
但只有一个参数,
数据库

对于标记的数量,您需要有足够的参数。最有可能的情况是,这是以下其中一个简单问题

  • 如果您试图使用另一个参数,则需要提供它,例如使用
    …%(数据库,表名)
  • 如果您试图使用literal
    %%
    标记,则需要将其转义为
    %%
    ,而不是您可能认为的
    \%%

如果你使用<代码>类似,我认为第二种可能性最大,因为<代码> %>代码>是该操作符的通配符。

以下文字记录可能会让问题更清楚:

pax> python
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> print "... -u root %s -e \"show tables like \'f_\%s\' ..." %("A")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

>>> print "... -u root %s -e \"show tables like \'f_\%s\' ..." %("A","B")
... -u root A -e "show tables like 'f_\B' ...

>>> print "... -u root %s -e \"show tables like \'f_%%\' ..." %("A")
... -u root A -e "show tables like 'f_%' ...

>>> _
pax>python
Python 2.7.3(默认,2014年3月14日,11:57:14)
[GCC 4.7.2]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>打印“…-u根%s-e\”以显示类似“f\%s\”…“%的表格(“A”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:格式字符串的参数不足
>>>打印“-u根%s-e\“显示类似于'f\%s\'…“%的表格”(“A”、“B”)
... -u root A-e“显示像'f\ub'这样的表。。。
>>>打印“-u根%s-e\“显示类似于'f\%%\'…“%”(“A”)的表格”
... -u根A-e“显示类似于“f_uz%”的表格。。。
>>> _
第一个和第二个命令显示,使用两个
%s
标记需要两个参数

第三个命令显示如何正确转义字符串中的
%

在格式字符串中有两个
%s
标记:

... -u root %s -e \"show tables like \'f_\%s\' ...
            ^^                            ^^
但只有一个参数,
数据库

对于标记的数量,您需要有足够的参数

  • 如果您试图使用另一个参数,则需要提供它,例如使用
    …%(数据库,表名)
  • 如果您试图使用literal
    %%
    标记,则需要将其转义为
    %%
    ,而不是您可能认为的
    \%%

如果你使用<代码>类似,我认为第二种可能性最大,因为<代码> %>代码>是该操作符的通配符。

以下文字记录可能会让问题更清楚:

pax> python
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> print "... -u root %s -e \"show tables like \'f_\%s\' ..." %("A")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

>>> print "... -u root %s -e \"show tables like \'f_\%s\' ..." %("A","B")
... -u root A -e "show tables like 'f_\B' ...

>>> print "... -u root %s -e \"show tables like \'f_%%\' ..." %("A")
... -u root A -e "show tables like 'f_%' ...

>>> _
pax>python
Python 2.7.3(默认,2014年3月14日,11:57:14)
[GCC 4.7.2]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>打印“-u根%s-e\“显示像'f\%s\'…“%”(“A”)这样的表”
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:格式字符串的参数不足
>>>打印“…-u根%s-e\”以显示类似“f\%s\”…“%的表格(“A”、“B”)
…-u root A-e“显示类似于'f\ub'的表格…”。。。
>>>打印“…-u根%s-e\”以显示类似“f\%%\'…”(“A”)的表格
…-u根A-e“显示类似于'f_uz%'的表格…”。。。
>>> _
第一个和第二个命令显示,使用两个
%s
标记需要两个参数

第三个命令显示如何正确转义字符串中的
%

在格式字符串中有两个
%s
标记:

... -u root %s -e \"show tables like \'f_\%s\' ...
            ^^                            ^^
但只有一个参数,
数据库

对于标记的数量,您需要有足够的参数。最有可能的情况是,这是以下其中一个简单问题

  • 如果您试图使用另一个参数,则需要提供它,例如使用
    …%(数据库,表名)
  • 如果您试图使用literal
    %%
    标记,则需要将其转义为
    %%
    ,而不是您可能认为的
    \%%

如果你使用<代码>类似,我认为第二种可能性最大,因为<代码> %>代码>是该操作符的通配符。

以下文字记录可能会让问题更清楚:

pax> python
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> print "... -u root %s -e \"show tables like \'f_\%s\' ..." %("A")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

>>> print "... -u root %s -e \"show tables like \'f_\%s\' ..." %("A","B")
... -u root A -e "show tables like 'f_\B' ...

>>> print "... -u root %s -e \"show tables like \'f_%%\' ..." %("A")
... -u root A -e "show tables like 'f_%' ...

>>> _
pax>python
Python 2.7.3(默认,2014年3月14日,11:57:14)
[GCC 4.7.2]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>打印“…-u根%s-e\”以显示类似“f\%s\”…“%的表格(“A”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:格式字符串的参数不足
>>>打印“-u根%s-e\“显示类似于'f\%s\'…“%的表格”(“A”、“B”)
... -u root A-e“显示像'f\ub'这样的表。。。
>>>打印“-u根%s-e\“显示类似于'f\%%\'…“%”(“A”)的表格”
... -u根A-e“显示类似于“f_uz%”的表格。。。
>>> _
第一个和第二个命令显示,使用两个
%s
标记需要两个参数

第三个命令显示如何正确转义字符串中的
%

在格式字符串中有两个
%s
标记:

... -u root %s -e \"show tables like \'f_\%s\' ...
            ^^                            ^^
但只有一个参数,
数据库

对于标记的数量,您需要有足够的参数

  • 如果您试图使用另一个参数,则需要提供它,例如使用
    …%(数据库,表名)
  • 如果您试图使用literal
    %%
    标记,则需要将其转义为
    %%
    ,而不是您可能认为的
    \%%

如果你使用<代码>类似,我认为第二种可能性最大,因为<代码> %>代码>是该操作符的通配符。

以下文字记录可能会让问题更清楚:

pax> python
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> print "... -u root %s -e \"show tables like \'f_\%s\' ..." %("A")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string

>>> print "... -u root %s -e \"show tables like \'f_\%s\' ..." %("A","B")
... -u root A -e "show tables like 'f_\B' ...

>>> print "... -u root %s -e \"show tables like \'f_%%\' ..." %("A")
... -u root A -e "show tables like 'f_%' ...

>>> _
pax>python
Python 2.7.3(默认,2014年3月14日,11:57:14)
[GCC 4.7.2]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>打印“-u根%s-e\“显示像'f\%s\'…“%”(“A”)这样的表”
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:格式字符串的参数不足
>>>打印“…-u根%s-e\”以显示类似“f\%s\”的表格…”(“A”、“B”