Python 字符串参数解析

标签 python

我正在使用 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/

相关文章:

python - Suds:响应中未找到类型

python - Str 对象不可调用; Python 字典循环错误

python - 将状态属性更改为 Tkinter 中的一组小部件

python - 具有相似主体的覆盖方法

python - 谷歌oauth登录Python : "Invalid parameter value for redirect_uri: Missing scheme: None"

python - 使用 asyncio 的简单 Python TCP fork 服务器

python - d3 不在 python Flask 脚本中加载 tsv 数据

python - Pylint in Sublime Text 3 不断弹出警告对话框 : pylint not found at "lint.py"

python - 如何选择模糊匹配算法?

python - 如何实时解决线程死锁?