gpt4 book ai didi

python - 标记命令字符串

转载 作者:太空宇宙 更新时间:2023-11-04 09:20:58 25 4
gpt4 key购买 nike

我有这样的字符串:

command ". / * or any other char like this" some_param="string param" some_param2=50

我想将这个字符串标记为:

command
". / * or any other char like this"
some_param="string param"
some_param2=50

我知道可以用空格分隔,但这些参数也可以用逗号分隔,例如:

command ". / * or any other char like this", some_param="string param", some_param2=50

我试着这样做:

\w+\=?\"?.+\"?

但是没用。

最佳答案

stdlib 模块 shlex 是为解析类似 shell 的命令语法而设计的:

>>> import shlex
>>> s = 'command ". / * or any other char like this" some_param="string param" some_param2=50'
>>> shlex.split(s)
['command', '. / * or any other char like this', 'some_param=string param', 'some_param2=50']

与您期望的结果唯一不同的是,引用的字符串作为字符串值返回,而不是引用的字符串文字。

关于python - 标记命令字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2723040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com