Python を使用してリストから変数を選択し、bash コマンドを使用して声に出して話そうとしています。今、私はこのようなものを持っています
foo = ["a","b","c","d"]
from random import choice
x = choice(foo)
foo.remove(x)
from os import system
system('say x')
これは「x」と表示されます。必要なのは、x
変数の値を表示することです。
私はあなたが使用できると思いますがos.system
、より良いかもしれませんsubprocess
:
import subprocess
subprocess.call(['say',x])
astring を渡している場合は、x の値を次のように使用できます
sytem('say {0}'.format(x))
文字列を渡すときは、文字列の書式設定を使用できます。お気づきのように、変数 x ではなく文字列で x の値を取得する必要があります http://docs.python.org/library/string.html#format-examples
フォーマットを使用できます:
system('say {}'.format(x))
文字列フォーマットを使用:
In [9]: x="ls -ld"
In [10]: os.system("{0}".format(x))
drwxr-xr-x 41 monty monty 4096 2012-10-10 22:46 .
Out[10]: 0