This entry was posted on 水曜日, 4月 16th, 2008 at 19:54:39 and is filed under Maya, python. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


system関数
テキスト抽出するコードをpythonで色々いじったのだが
結局sedとgrepを使うシェルスクリプトを書いた方が早かった。
pythonでテキストを扱うライブラリとかあるのかな?
pythonのos.systemでgrepを実行した場合
windowsだと常に0が返るので、結果を受け取れないのだ。
結果が欲しい場合、リダイレクションさせて一回ファイルに落とさないといけない
os.system("grep '***' './script.txt' > temp.txtx")
mynzさんに指摘してもらって、パイプを使ったらうまくいった
os.popen("grep '***' './script.txt'").read().split("\n")
MELのsystem関数だとちゃんと結果が返ってきて
stringに格納できる
string $result = system("grep '***' './script.txt'")
2 Responses to “system関数”
Leave a Reply

4月 17th, 2008 at 00:56:26
外部コマンドを使ってそのプロセス戻り値ではなく、標準出力を受け取りたい場合はパイプを使うのがいいのでは。
Pythonでは、以下の感じかな?
result = os.popen(”ls”).read()
grepとsedでコトが足りるのなら、Pythonの組み込み系と正規表現で何とかなるかもね。
4月 17th, 2008 at 11:06:58
おお出来ました!
ありがとうございます!
これでいちいちファイルにリダクションしなくてよくなりました!