Skip to main content
 首页 » 编程设计

python之如何将命令行字符串转换为数组

2025年05月04日15wuhuacong

我在一个字符串变量中有一个命令行(这不是我程序的参数,我从别的地方得到的):

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 中有等效项吗?

请您参考如下方法:

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