从命令行开始工作但是当我在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!
答案 0 :(得分:2)
你在其中一个sed
中看到了这个东西:
... | sed -e 's/%]//g' | ...
将该位更改为:
... | sed -e 's/%%]//g' | ...
这就是%
中python
字符串中{{1}}的转义。