2011-02-17 100 views
3

我正在使用python中的cmd類,它將所有參數作爲一個大字符串傳遞給我。將arg字符串標記爲args []數組的最佳方法是什麼?Python字符串參數解析

例子:

args = 'arg arg1 "arg2 with quotes" arg4 arg5=1' 
result = split_args(args) 

它會是什麼樣子:

result = [ 
     'arg', 
     'arg1', 
     'arg2 with quotes', 
     'arg4', 
     'arg5=1' 
] 

回答

11
import shlex 
shlex.split('arg arg1 "arg2 with quotes" arg4 arg5=1') 
+0

那石頭,我知道這將是簡單的...感謝。 – Nix 2011-02-17 22:59:53