Python - 如何修复此行类型错误:格式字符串没有足够的参数

时间:2013-12-08 20:01:51

标签: bash awk

从命令行开始工作但是当我在Python中包含这个

终端:

$ amixer -c2 | grep "Simple mixer control 'Mic',0" -A 5 | grep "Mono: " | sed -e 's/Capture /\n/g' | tail -1 | awk '{print $2}' | sed -e 's/%]//g' | sed -e 's/\[//g'
88

的Python:

tmp = "2"
a = """amixer -c%s | grep "Simple mixer control 'Mic',0" -A 5 | grep "Mono: " | sed -e 's/Capture /\n/g' | tail -1 | awk '{print $2}' | sed -e 's/%]//g' | sed -e 's/\[//g'""" % tmp
print "Reply " + a
a = os.popen(a).read()
print a

错误:

Running: /var/tmp/p/test.py (Sun Dec  8 20:58:07 2013)


Traceback (most recent call last):
  File "/var/tmp/p/test.py", line 2, in <module>
    a = """amixer -c%s | grep "Simple mixer control 'Mic',0" -A 5 | grep "Mono: " | sed -e 's/Capture /\n/g' | tail -1 | awk '{print $2}' | sed -e 's/%]//g' | sed -e 's/\[//g'""" % tmp
TypeError: not enough arguments for format string


Execution Successful!

1 个答案:

答案 0 :(得分:2)

你在其中一个sed中看到了这个东西:

... | sed -e 's/%]//g' | ...

将该位更改为:

... | sed -e 's/%%]//g' | ...

这就是%python字符串中{{1}}的转义。