我正在使用 python 中的 cmd 类,它将我的所有参数作为一个大字符串传递给我。将此 arg 字符串标记为 args[] 数组的最佳方法是什么。
例子:
args = 'arg arg1 "arg2 with quotes" arg4 arg5=1'
result = split_args(args)
它看起来像:
result = [
'arg',
'arg1',
'arg2 with quotes',
'arg4',
'arg5=1'
]
最佳答案
import shlex
shlex.split('arg arg1 "arg2 with quotes" arg4 arg5=1')
关于Python 字符串参数解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5035702/