2015-11-03 48 views
1

我有一个字符串变量命令行(这不是我的程序的参数,我得到了它在其他地方):如何将命令行字符串转换为数组?

cmd_line = "app.exe --a value -b \"this is a quoted string\""; 

我怎么能这个命令行转换成数组?

res = ["app.exe", "--a", "value", "-b", "this is a quoted string"] 

有一个winapi函数CommandLineToArgvW。 Python中有一个等价的东西吗?

回答

1

shlex.split()正是这么做的:https://docs.python.org/2/library/shlex.html

+0

感谢。但是这有时会在Windows上失败,因为特殊字符,如'>','<'在Windows上以不同方式转义。 – zenden2k

+0

我的不好,我忽略了你的问题是Windows特有的事实。 – NPE

相关问题